R use value of a variable as a data frame column name -
i guessing simple experienced used...how can use value of variable assign data frame column name? have simple data frame df below, , variable n changes value based on user input. how insert new data frame column has it's name value of n? ideally concatenate value of n simple string. thank you.
df<-data.frame(a=c(1,1,1),b=c(2,2,2)) b 1 1 2 2 1 2 3 1 2 when try assign new column
n<-15 df$n<-c(3,3,3) the name of column n.
b n 1 1 2 3 2 1 2 3 3 1 2 3
it's not best idea name column number, work:
df[,paste(n)] <- c(3,3,3)
Comments
Post a Comment