0

I have a dataframe "old_df" and I would like to refer to its columns with "i" in a for loop and do some operations (the operation below is only an example).

v1 <- c(1,2,3)
v2 <- c(4,5,6)
old_df = data.frame(Price=v1, Value=v2)

colnames = c("Price", "Value")
new_df = data.frame(Price=numeric(), 
                 Value=numeric()) 

for (i in colnames) {
    new_df$i <- old_df$i
}

However referencing this way (old_df$i) does not work. Writing old_df$"Price" into the console gives back the correct column, writing i gives back label "Price", however old_df$i gives back NULL. What is the problem here?

Balázs Fehér
  • 344
  • 5
  • 14

0 Answers0