Data:
structure(list(`p value` = c(0.00151124736422317, 0.804709799937324,
0.0192537412780042, 0.000467854188597731, 4.80216666553605e-06,
0.0231434946595433), significance = c(TRUE, FALSE, TRUE, TRUE,
TRUE, TRUE)), .Names = c("p value", "significance"), row.names = c("Q5.i",
"Q5.ii", "Q5.iii", "Q5.iv", "Q5.v", "Q5.vi"), class = "data.frame")
Objective: To create a function that would take input of dataframe name and a (new) variabe name. The function would:
- create a new variable that is based on the row name of the dataframe
- delete the row name
- reorder the variable so that the newly created column is first column
Challenges: I am stuck at the first step.
I've searched the internet and stackoverflow for snippets of code that would help and I've managed to hammer something although it couldn't work. What have I tried:
row2col<-function(df, varname){
eval(parse(text=paste(df, "[[", "'", varname, "'", "]]", "<-row.names(", df, ")", sep="")))
}
row2col<-function(df, varname){
assign(parse(text=paste(df, varname, sep="$")), row.names(df))
}
Results:
- nothing happened (not even an error message)
- a character vector of row names (rather than a variable within the dataframe) was created
Thanks for your help and attention to this post.