简体   繁体   中英

Use loop iteration to dynamically update dataframe name in ddply?

I want to use ddply in a loop, but need to change the data name based on the loop iteration, such as:

iteration 1: ddply(df1, ... iteration 2: ddply(df2, ... iteration 3: ddply(df3, ...

I've tried using something like paste0("df",j) in place of the data name, but that doens't work. I've also tried

data <- paste0("df",j)
ddply(data, ...

but that doesn't work either. Does anyone know if this is possible? Also, apologies for not preparing a working example, i'm actually working with dataframes stored in a list which would make for a complicated example. However, I could try and distill it down into a working example if necessary.

If you have a list dfs like in comments, containing df1 df2 etc., then I imagine you could do something like

sapply(dfs,function(d) ddply(d,...))

Exactly application depends on your exact usage.

You may want to use lapply here instead, as it seems to give a better result format:

> dfx1 <- data.frame(
+   group = c(rep('A', 6), rep('B', 14), rep('C', 9)),
+   sex = sample(c("M", "F"), size = 29, replace = TRUE),
+   age = runif(n = 29, min = 18, max = 54)
+ )
> 
> dfx2 <- data.frame(
+   group = c(rep('A', 6), rep('B', 14), rep('C', 9)),
+   sex = sample(c("M", "F"), size = 29, replace = TRUE),
+   age = runif(n = 29, min = 18, max = 54)
+ )
> 
> dfs <- list(dfx1,dfx2)
> 
> v <- lapply(dfs,function(d) ddply(d,.(group,sex),summarize,agemean=mean(age)))
> v
[[1]]
  group sex  agemean
1     A   F 26.76541
2     A   M 29.16023
3     B   F 37.63099
4     B   M 39.92033
5     C   F 35.24655
6     C   M 37.86241

[[2]]
  group sex  agemean
1     A   F 36.68879
2     A   M 36.55951
3     B   F 40.56872
4     B   M 37.60044
5     C   F 33.41869
6     C   M 42.35216

> 

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