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.

talat
  • 68,970
  • 21
  • 126
  • 157
Raphael Lee
  • 111
  • 1
  • 10
  • 1
    R rule of thumb - if you are ever using `eval` `assign` `parse`.... don't. :-) – thelatemail Jun 28 '16 at 06:34
  • This function exists already, as [`dplyr::add_rownames()`](https://github.com/hadley/dplyr) and [`tibble::rownames_to_column()`](https://github.com/hadley/tibble/) – alistaire Jun 28 '16 at 06:53
  • @alistaire Thanks for your sharing. I use the dplyr package extensively and didn't even notice its already in there. – Raphael Lee Jun 28 '16 at 06:55

2 Answers2

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
talat
  • 68,970
  • 21
  • 126
  • 157
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))]
mkearney
  • 1,266
  • 10
  • 18