0

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:

  1. create a new variable that is based on the row name of the dataframe
  2. delete the row name
  3. 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:

  1. nothing happened (not even an error message)
  2. a character vector of row names (rather than a variable within the dataframe) was created

Thanks for your help and attention to this post.

3
  • 1
    R rule of thumb - if you are ever using eval assign parse.... don't. :-) Commented Jun 28, 2016 at 6:34
  • This function exists already, as dplyr::add_rownames() and tibble::rownames_to_column() Commented Jun 28, 2016 at 6:53
  • @alistaire Thanks for your sharing. I use the dplyr package extensively and didn't even notice its already in there. Commented Jun 28, 2016 at 6:55

2 Answers 2

4

You don't need to use eval, parse, assign - that's in many cases not the right approach. Here's a simple alternative:

row2col <- function(dat, varname) {
  dat[[varname]] <- row.names(dat)  
  row.names(dat) <- NULL
  dat[, c(varname, setdiff(names(dat), varname))]
}

And then you can test it:

> row2col(df, "testcol")
#  testcol      p value significance
#1    Q5.i 1.511247e-03         TRUE
#2   Q5.ii 8.047098e-01        FALSE
#3  Q5.iii 1.925374e-02         TRUE
#4   Q5.iv 4.678542e-04         TRUE
#5    Q5.v 4.802167e-06         TRUE
#6   Q5.vi 2.314349e-02         TRUE
Sign up to request clarification or add additional context in comments.

1 Comment

And I struggled for half a day with the first objective only...thanks very very much!!!
0

Create new var using row names.

data$new_var <- row.names(data)

Reset row names

row.names(data) <- NULL

Reorder data frame with new var first

data <- data[, c(ncol(data):(ncol(data) - 1))]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.