Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through a column in R

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:

  1. I can't get it to work
  2. 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?
like image 823
Andy Avatar asked Oct 25 '09 19:10

Andy


1 Answers

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.

like image 76
Matt Parker Avatar answered Sep 18 '22 22:09

Matt Parker