Create a new column based on an index column with error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Create a new column based on an index column with error



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


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



How to convert the data type into the right class?





Please format you question and make it easier on eyes.
– Arash Kazemi
1 hour ago





Noted...I try harder next time...thanks
– kengfui
1 hour ago





You may want to share your sample data using dput(head(df))
– Prem
1 hour ago


dput(head(df))





Getting better....thanks for the input.
– kengfui
49 mins ago




1 Answer
1



I got it finally....



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



I did a as.data.frame conversion to
[1] "data.frame"



Thanks guys






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived