0

I try to copy a column within a dataframe. Next I try to rename the columnname in the code below although I do not succeed.

library(dplyr)

d <- data.frame(red=1:3, green=4:6, yellow=7:9)

for (q in 1:3) {
            
  d$color <- select(d, q)
  
  colnames(d)[4] <- "newcolor"

}  
 

First I try to change the name with d$newcolor which does not succeed. Next I try using colnames which does not succeed either. Although this works fine on the first 3 columns.

I need a solution with identifying the columns by their numbers since I need it as part of a loop. (I actually try to acomplish to fill the 4th column every loop with a different source column)

Thanks a lot!

aynber
  • 22,380
  • 8
  • 50
  • 63
user2165379
  • 445
  • 4
  • 20
  • Is there a particular reason you want to (1) create `color` with q=1, and then (2) overwrite `color` with q=2, and then (3) overwrite `color` with q=3? In short, `select(d,q)` is returning a `tbl`, not a column, so either `d[[q]]` or `pull(d,q)` will return a vector. But your new-column creation/naming is fragile and suspect. – r2evans Dec 27 '20 at 17:02
  • @ r2evans Thanks. The reason I do (1) and (2) is that later in my code I want to fit a gam with a different variable for every loop. I noticed there is a method with splitting a string within the gam function although I thought this would be easier to write. I understand this method is fragile when my columnnames would change. Is this the risk you mean? Thank you. I see pull and d[[q]] both do what I need. – user2165379 Dec 27 '20 at 17:55
  • @ r@evans While using R I am most often stuck in problems like above where the rootcause is a conflict between object types or data types. It seems I am mixing these types as input and output in my code where this is not possible. Is there a book or other source you can advice to overcome these errors? I have read R for Data Science, Practical data science with R and An Introduction to statistical learning with applications in R and a course data science from Coursera. Thanks! – user2165379 Dec 28 '20 at 11:34

1 Answers1

0

Try this: using dplyr package

library(dplyr)

d <- data.frame(red=1:3, green=4:6, yellow=7:9)
d%>%mutate(newcolor=yellow)

without dplyr

d <- data.frame(red=1:3, green=4:6, yellow=7:9) 
d$somename<-d$yellow
colnames(d)[4]<-"newcolor"

To my limited knowledge, dplyr has its own syntax. We cannot always combine it with base R.

ssaha
  • 459
  • 2
  • 10