Hi All, I have a data frame with a group of variables named b1, b2, b3, ..., b18. These variables take the value 1, 2 or NA. For each observation, I want to do some computation by looping over the values for the group of variables: b1 to b18. In STATA I would do: forval i=1/18 { --- use b`i' for computation ---- } How can this be done in R? Deepankar
You can use 'get': for (i in 1:18){ .temp <- get(paste('b', i, sep='')) # get data to use ..... now use .temp in rest of calculations } On 10/24/07, DEEPANKAR BASU <basu.15 at osu.edu> wrote:> Hi All, > > I have a data frame with a group of variables named b1, b2, b3, ..., b18. These variables take the value 1, 2 or NA. For each observation, I want to do some computation by looping over the values for the group of variables: b1 to b18. In STATA I would do: > > forval i=1/18 { > --- use b`i' for computation ---- > } > > How can this be done in R? > > Deepankar > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Actually I forgot you have a data frame. so use: for (i in 1:18){ .temp <- yourDataFrame[[paste('b', i, sep='')]] .... use .temp .... } On 10/24/07, DEEPANKAR BASU <basu.15 at osu.edu> wrote:> Hi All, > > I have a data frame with a group of variables named b1, b2, b3, ..., b18. These variables take the value 1, 2 or NA. For each observation, I want to do some computation by looping over the values for the group of variables: b1 to b18. In STATA I would do: > > forval i=1/18 { > --- use b`i' for computation ---- > } > > How can this be done in R? > > Deepankar > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
I think that if you want to apply a simple calculation to each element something like df[,3:7] <- df[,3:7] + 10 would do what you want. --- DEEPANKAR BASU <basu.15 at osu.edu> wrote:> Hi All, > > I have a data frame with a group of variables named > b1, b2, b3, ..., b18. These variables take the value > 1, 2 or NA. For each observation, I want to do some > computation by looping over the values for the group > of variables: b1 to b18. In STATA I would do: > > forval i=1/18 { > --- use b`i' for computation ---- > } > > How can this be done in R? > > Deepankar > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >