0

I have a script in R which I am attempting to use to separate several data frames by a certain variable. I am currently using a loop to do this and wondering if I can instead use lapply or a similar function.

The loop essentially takes a data frame, df, which has a column called Time. The time values are in hours and range from 0-48 by multiples of six (which is what the index list contains).

The code should create a new dataframe called data.time.0 consisting of all rows where time = 0, and so on for every value of time.

library(tidyverse)

index = seq(from = 0, to = 48, by = 6)

for (i in index) {
  name = paste("data.time."+i,sep = "")
  currentdf = filter(df,df$time == i)
  assign(name,currentdf)
}

However, I have heard that using assign should be avoided when possible, and I would like to use a vector operation rather than a loop. Is this possible? How would I do it?

4
  • 5
    split(df, df$time)? Commented Feb 27, 2019 at 15:53
  • 2
    Take @Roland's advice; much better to use split() and get a list than use assign(). Commented Feb 27, 2019 at 15:54
  • I did not know that split() existed. So if I do split(df,df$time)<-paste("data.time.", seq(from = 0, to = 48, by = 6),sep = "") this will give me a list containing all the data frames I want? Commented Feb 27, 2019 at 16:00
  • 1
    No; you'd save the result from split into a new variable: split_df <- split(df,df$time). That new variable will be a list of data frames. I believe the default names will be the values from df$time (as strings). Commented Feb 27, 2019 at 16:04

1 Answer 1

1

A direct translation to lapply() would be

mylist <- lapply(seq(from = 0, to = 48, by = 6), function(x){
  filter(df, df$time == x)
})

names(mylist) <- paste("data.time.", seq(from = 0, to = 48, by = 6),sep = "")

I agree with @Roland's comment, though. It is very likely that there's a simpler approach.

Sign up to request clarification or add additional context in comments.

2 Comments

If I do this either way and get a list of data frames is it possible to use lapply to do aov on each data frame in the list? My guess is that there is an easier, better way to do this whole operation but I do not know what it is. I do not know enough about Tidyverse to know if it has a built-in way of doing this.
lapply(mylist, aov)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.