简体   繁体   中英

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?

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.

如果parse.smiles()是一个你想要应用于矢量“vec”的所有条目的函数,那么你可以使用:

lapply(1:length(vec),parse.smiles(vec[i]))

The only reason looping is discouraged is that it is slow. R is designed to work on vectors at a time and has lots of functions to accomplish this. The whole apply family, as well as functions like Vectorize to help out. So the idiom is that if your using for loops you're not thinking in R, but sometimes for loops really are just appropriate.

To do this in the R way of thinking, Vectorize your function, if it is not already vectorized (see the Vectorize function) then call that function with the entire column as an argument and assign that to the new column.

f<-Vectorize(function(x,...),'x')
data$newcolumn<-f(data[,1])

The apply family (apply, sapply, lapply, mapply, tapply) are also alternatives. Most native R functions are already vectorized, but be careful when passing extra arguments that are supposed to be interpreted as vectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM