-1

I am referring to a previous question:- Create a new column based on an index column

I've tried few conversions to convert into tibble but still hitting the following error:

> Can't use matrix or array for column indexing

Need to create the NewCol based on the colnumber in SecondLast

Here the sample data:-

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"))

This is the code I used:

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

How to convert the data type into the right class?

Sam
  • 644
  • 4
  • 21
kengfui
  • 1
  • 2

1 Answers1

0

I got it finally....

The initial class for my table is "tbl_df" "tbl" "data.frame"

I did a as.data.frame conversion to "data.frame"

Thanks guys

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
kengfui
  • 1
  • 2