-1

I need to know how can I create in R indexed variables in a data.frame.

Example: I have P1, P2, P3, B1, B2, B3 numeric variables in a data.frame (dat) and I have to create the new variables: I1=P1/B1, I2=P2/B2 and I3=P3/B3 in the data.frame dat using a for loop.

Maybe I was not clear, sorry. I have to create variables (100) Ik, And I don't want to write:

dat$I1<-dat$P1/dat$B1  
dat$I2<-dat$P2/dat$B2
dat$I3<-dat$P3/dat$B3    
...
dat$I99<-dat$P99/dat$B99
dat$I100<-dat$P100/dat$B100

It's surely possible to do something like that:

for(k in 1:100) {
    ???
}

Thanks a lot!

vrajs5
  • 4,066
  • 1
  • 27
  • 44
  • Don't do this with a `for` loop. If you like `for` loops, you should use a different language. – Roland May 26 '14 at 14:45
  • 2
    Welcome to StackOverflow! I did give you an answer already, but for the future: Please read the info about [how to ask a question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Also share any code that you’ve tried so far. This will make it much easier for others to help you. – Jaap May 26 '14 at 14:59

3 Answers3

2

You could simply use the following code to create your new variables:

# creating a example dataframe
dat <- data.frame(P1=rnorm(100,40,4), P2=rnorm(100,20,2), P3=rnorm(100,10,1),
                  B1=rnorm(100,10,2), B2=rnorm(100,5,1), B3=rnorm(100,2.5,1))

# creating the new variables
dat$I1 <- dat$P1/dat$B1
dat$I2 <- dat$P2/dat$B2
dat$I3 <- dat$P3/dat$B3

EDIT: extending on @agstudy's answer:

# creating a new dataframe based on @agstudy's
nn <- colnames(dat)
i123 <- mapply(function(x,y)dat[,x]/dat[,y],grep('P',nn),grep('B',nn))
i123 <- as.data.frame(i123)
colnames(i123) <- c("I1","I2","I3")

# adding the dataframe with the new variables to the existing dataframe
dat <- cbind(dat,i123)
Jaap
  • 81,064
  • 34
  • 182
  • 193
2

Could do it easily with data.table

library(data.table)
setDT(dat)[, c("I1", "I2", "I3") := list(P1/B1, P2/B2, P3/B3)]
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
1

In R , we try to avoid using for loops essentially because of their side effect. You should use the R way to do things like:

  • Using vectorized operations
  • Using one of many xxapply family functions

For example , here using mapply, I get a generalised version of @Jaap answer:

nn <- colnames(dat)
mapply(function(x,y)dat[,x]/dat[,y],grep('P',nn),grep('B',nn))

PS: there are some situations where we have no choice and we use a for loop, specially where there are some recursion relation between iterations.

agstudy
  • 119,832
  • 17
  • 199
  • 261