Reputation: 43
My data frame is called Subs.
My variables are REV_4
, REV_5
, REV_6
etc
I want to create new variables to calculate percentage change of revenue.
Eg: d.rev.5 <- Subs$REV_5/Subs/$REV_4 -1
I would like to use a loop to create these new variables. I've tried this:
for(i in 5:10){
Subs$d.data.[i] <- Subs$REV_[i]/Subs$REV_[i-1] - 1 }
But it doesn't work.
I suspect it's not recognizing the i
as part of the variable name.
Is there any way to get around this? Thank you so much.
Upvotes: 1
Views: 6238
Reputation: 26258
You can't reference columns like you're attempting (Subs$REV_[i]
), you need to create a string to represent the column.
What I think you're trying to do is (in the absense of your data I've created my own)
set.seed(123)
Subs <- data.frame(rev_1 = rnorm(10, 0, 1),
rev_2 = rnorm(10, 0, 1),
rev_3 = rnorm(10, 0, 1),
rev_4 = rnorm(10, 0, 1))
for(i in 2:4){
## looping over columns 2-4
col1 <- paste0("rev_", i)
col2 <- paste0("rev_", i - 1)
col_new <- paste0("d.rev.", i)
Subs[, col_new] <- Subs[, col1] / Subs[, col2]
}
## A note on subsetting a data.frame
Subs$rev_1 ## works
i <- 1
Subs$rev_[i] ## doesn't work
Subs[, rev_[i]] ## doesn't work
Subs[, "rev_1"] ## works
Subs[, paste0("rev_", i)] ## works
## because
paste0("rev_", i) ## creates the string:
[1] "rev_1"
Upvotes: 2