How to smooth a nested list into a single list more efficiently and not use the list method?

I have a nested list containing a set of data.frame objects in it, now I want them to be smoothed out. I used the most common approach, for example, the unlist method, it did not adjust my list, the result was not sufficiently presented. How can I do this more efficiently? Does anyone know of any trick in performing this operation? Thank.

Example:

mylist <- list(pass=list(Alpha.df1_yes=airquality[2:4,], Alpha.df2_yes=airquality[3:6,],Alpha.df3_yes=airquality[2:5,],Alpha.df4_yes=airquality[7:9,]),
             fail=list(Alpha.df1_no=airquality[5:7,], Alpha.df2_no=airquality[8:10,],  Alpha.df3_no=airquality[13:16,],Alpha.df4_no=airquality[11:13,]))

I tried like this: it works, but the result was not arranged correctly.

res <- lapply(mylist, unlist)

after smoothing, I would like to combine them without duplication:

out <- lapply(res, rbind.data.frame)

my desired result:

mylist[[1]]$pass:
  Ozone Solar.R Wind Temp Month Day
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4

How to make this kind of anti-aliasing more compatible? Can anyone suggest a possible idea to do this in R? Many thanks.

+4
1

:

res <- lapply(mylist, function(i){
  x <- do.call(rbind, i)
  x[ !duplicated(x), ]
  rownames(x) <- NULL
  x
})

res$pass
#    Ozone Solar.R Wind Temp Month Day
# 1     36     118  8.0   72     5   2
# 2     12     149 12.6   74     5   3
# 3     18     313 11.5   62     5   4
# 4     12     149 12.6   74     5   3
# 5     18     313 11.5   62     5   4
# 6     NA      NA 14.3   56     5   5
# 7     28      NA 14.9   66     5   6
# 8     36     118  8.0   72     5   2
# 9     12     149 12.6   74     5   3
# 10    18     313 11.5   62     5   4
# 11    NA      NA 14.3   56     5   5
# 12    23     299  8.6   65     5   7
# 13    19      99 13.8   59     5   8
# 14     8      19 20.1   61     5   9

, , :

res <- do.call(rbind, unlist(mylist, recursive = FALSE))
res <- res[!duplicated(res), ]
res
#                      Ozone Solar.R Wind Temp Month Day
# pass.Alpha.df1_yes.2    36     118  8.0   72     5   2
# pass.Alpha.df1_yes.3    12     149 12.6   74     5   3
# pass.Alpha.df1_yes.4    18     313 11.5   62     5   4
# pass.Alpha.df2_yes.5    NA      NA 14.3   56     5   5
# pass.Alpha.df2_yes.6    28      NA 14.9   66     5   6
# pass.Alpha.df4_yes.7    23     299  8.6   65     5   7
# pass.Alpha.df4_yes.8    19      99 13.8   59     5   8
# pass.Alpha.df4_yes.9     8      19 20.1   61     5   9
# fail.Alpha.df2_no.10    NA     194  8.6   69     5  10
# fail.Alpha.df3_no.13    11     290  9.2   66     5  13
# fail.Alpha.df3_no.14    14     274 10.9   68     5  14
# fail.Alpha.df3_no.15    18      65 13.2   58     5  15
# fail.Alpha.df3_no.16    14     334 11.5   64     5  16
# fail.Alpha.df4_no.11     7      NA  6.9   74     5  11
# fail.Alpha.df4_no.12    16     256  9.7   69     5  12
+4

Source: https://habr.com/ru/post/1649503/


All Articles