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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With