R: loop through data frame extracting subset of data depending on date

后端 未结 5 1654
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 14:29

I have a large data frame that consists of data that looks something like this:

        date    w    x    y    z    region
1    2012 01    21   43   12    3   NO         


        
相关标签:
5条回答
  • 2021-02-06 14:40

    is this what you want ? df_list <- split(data, as.factor(data$date))

    0 讨论(0)
  • 2021-02-06 14:47

    Loop through each unique date and build the subset.

    uniq <- unique(unlist(data$Date))
    for (i in 1:length(uniq)){
        data_1 <- subset(data, date == uniq[i])
        #your desired function
    }
    
    0 讨论(0)
  • 2021-02-06 14:52

    After sub-setting your dataset by date, imagine that the function you would like to apply to each subset is to find the mean of the column x. You could do it this way: (df is your dataframe)

     library(plyr)
     ddply(df, .(date), summarize, mean = mean(x))
    
    0 讨论(0)
  • 2021-02-06 14:54

    You can split your data.frame into a list of data.frames like this:

    list.of.dfs<-by(data,data$date)
    
    0 讨论(0)
  • 2021-02-06 14:54

    This is a perfect situation for the plyr package:

    require(plyr)
    ddply(my_df, .(date), my_function, extra_arg_1, extra_arg_2)
    

    where my_function is the function you want to perform on the split data frames, and extra_args are any extra arguments that need to go to that function.

    ddply (data frame -> data frame) is the form you want if you want your results in a data frame; dlply returns a list.

    0 讨论(0)
提交回复
热议问题