Raphael Lee
Raphael Lee

Reputation: 111

Executing a function with paste to create a new variable in a dataframe in R

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.

Upvotes: 0

Views: 867

Answers (2)

mkearney
mkearney

Reputation: 1345

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

Upvotes: 0

talat
talat

Reputation: 70336

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

Upvotes: 4

Related Questions