Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can R loop over data frames?

Tags:

for-loop

r

Suppose there are many data frames that need the same operation performed on them. For example:

prefix <- c("Mrs.","Mrs.","Mr","Dr.","Mrs.","Mr.","Mrs.","Ms","Ms","Mr")
measure <- rnorm(10)
df1 <- data.frame(prefix,measure)
df1$gender[df1$prefix=="Mrs."] <- "F"

Would create an indicator variable called gender when the value in the adjacent row was "Mrs.". A general way to loop over string variables in R was adapted from here with the function as.name() added to remove the quotes from "i":

dflist <- c("df1","df2","df3","df4","df5")

for (i in dflist) { 
  as.name(i)$gender[as.name(i)$prefix=="Ms."] <- "F"
  }

Unfortunately this doesn't work. Any suggestions?

like image 785
hubert_farnsworth Avatar asked Jul 13 '13 20:07

hubert_farnsworth


People also ask

Can you loop through a Dataframe in R?

You certainly could! Before you do so, note that you can get the number of rows in your data frame using nrow(stock) . Then, you can create a sequence to loop over from 1:nrow(stock) .

Does R use for loops?

There are three types of loop in R programming: For Loop. While Loop. Repeat Loop.


2 Answers

The single instance example would not really create an indicator in the usual sense since the non-"F" values would be <NA> and those would not work well within R functions. Both arithmetic operations and logical operations will return . Try this instead:

  df1$gender <- ifelse(prefix %in% c("Mrs.", "Ms") , "probably F",
                ifelse( prefix=="Dr.", "possibly F",  # as is my wife.
                                       "probably not F"))

Then follow @HongDoi's advice to use lists. And do not forget to a) return a full dataframe-object , and b) assign the result to an object name (both of which were illustrated but often forgotten by R-newbs.)

like image 23
IRTFM Avatar answered Oct 24 '22 10:10

IRTFM


Put all your data frames into a list, and then loop/lapply over them. It'll be much easier on you in the long run.

dfList <- list(df1=df1, df2=df2, ....)

dfList <- lapply(dfList, function(df) {
    df$gender[df$prefix == "Mrs."] <- "F"
    df
})

dfList$df1
like image 195
Hong Ooi Avatar answered Oct 24 '22 10:10

Hong Ooi