【问题标题】:Create a new column based on an index column with error基于有错误的索引列创建新列
【发布时间】:2018-07-27 06:15:05
【问题描述】:

我指的是上一个问题:- Create a new column based on an index column

我尝试了几次转换以转换为 tibble,但仍然遇到以下错误:

> Can't use matrix or array for column indexing

Need to create the NewCol based on the colnumber in SecondLast

这里是示例数据:-

structure(list(L1;L2;L3;L4;L5;L6;L7;L8;L9;L10;SecondLast;NewCol = c("10001;1;NA;NA;NA;NA;NA;NA;NA;NA;1;10001", 
"10005;1;NA;NA;NA;NA;NA;NA;NA;NA;1;10005", "10006;10001;1;NA;NA;NA;NA;NA;NA;NA;2;10001", 
"10007;10003;1;NA;NA;NA;NA;NA;NA;NA;2;10003", "10008;1;NA;NA;NA;NA;NA;NA;NA;NA;1;10008", 
"10009;10004;1;NA;NA;NA;NA;NA;NA;NA;2;10009", "10021;10005;1;NA;NA;NA;NA;NA;NA;NA;2;10005", 
"10022;1;NA;NA;NA;NA;NA;NA;NA;NA;1;10022", "10023;10055;10182;10201;10164;10088;10049;10027;10022;1;9;10022", 
"10024;10004;1;NA;NA;NA;NA;NA;NA;NA;2;10004", "10025;10055;10182;10201;10164;10088;10049;10027;10022;1;9;10022", 
"10030;10010;10003;1;NA;NA;NA;NA;NA;NA;3;10003", "10031;10043;10056;10020;10003;1;NA;NA;NA;NA;5;10003"
)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(`L1;L2;L3;L4;L5;L6;L7;L8;L9;L10;SecondLast;NewCol` = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector"))), class = "col_spec"))

这是我使用的代码:

df$Leader <- df [1:30][cbind(seq_len(nrow(df)), df$SecondLast)]

如何将数据类型转换为正确的类?

【问题讨论】:

  • 请格式化您的问题并使其更容易理解。
  • 注意...下次我会更努力...谢谢
  • 您可能希望使用dput(head(df))分享您的样本数据
  • 越来越好....感谢您的意见。

标签: r indexing cbind


【解决方案1】:

我终于明白了……

table 的初始类是 "tbl_df" "tbl" "data.frame"

我将as.data.frame 转换为 "data.frame"

谢谢大家

【讨论】:

    猜你喜欢
    • 2018-04-03
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多