Looping through a column in R

loops, r, statistics

Solution

Without any examples, it's hard to know how to respond. The basic case of what you're describing, however, is this:

#Just a very simple data frame
dat <- data.frame(x = c(1, 2, 3))
#Compute the squared value of each value in x
dat$y <- dat$x^2
#See the resultant data.frame, now with column y
dat

When you tell R to square a vector (or vector-like structure, like dat$x), it knows to square each value separately. You don't need to explicitly loop over those values most of the time - although, as Dirk notes, you should only worry about optimizing your loops if they are causing you problems. That said, I certainly prefer reading and writing

dat$y <- dat$x^2

to:

for(i in 1:length(dat$x)){
  dat$y[i] <- dat$x[i]^2
}

... where possible.

Problem

I am using the `R's stats` package and would like to loop through `column[x]` in `all the rows of a dataframe`, operate on the data in `each cell` in the column with a function and pass the result to a new column (with the `calculated result` in the `new column` aligned with the data in `column[x]`) I've got two problems: - I can't get it to work - looping seems to be discouraged in the `R articles` I've read. Is there an alternative approach and if not, does anyone have an example of how to carry out the loop?

Original source