I’ve been given a nested list where each sublist contains information about values of one specific data frame. The list has the following structure:
summary <- list(df1 = list(value1 = "phone", count = 11, ratio = 78.57, value2 = "mail", count = 13, ratio = 92.86, value3 = "zoom", count = 8, ratio = 57.14),
df2 = list(value4 = "yes", count = 4, ratio = 28.57, value5 = "no", count = 10, ratio = 71.43))
str(summary)
List of 2
$ df1:List of 9
..$ value1: chr "phone"
..$ count : num 11
..$ ratio : num 78.6
..$ value2: chr "mail"
..$ count : num 13
..$ ratio : num 92.9
..$ value3: chr "zoom"
..$ count : num 8
..$ ratio : num 57.1
$ df2:List of 6
..$ value4: chr "yes"
..$ count : num 4
..$ ratio : num 28.6
..$ value5: chr "no"
..$ count : num 10
..$ ratio : num 71.4
Here, summary[[1]]
states that the value “phone” occurred eleven times in data frame 1 with a certain ratio, the value “mail” occurred 13 times and so on. The same applies for data frame 2 where “yes” was counted four times etc.
Now, i want to create a nested list, that summarizes for each sublist in summary the values, counts, and ratios. More precisely, each sublist of the result list should only consist of three elements value, count and ratio, each containing the values, corresponding counts and ratios. The desired list result should have the following structure:
result <- list(res_df1 = list(value = c("phone", "mail", "zoom"), count = c(11,13,8), ratio = c(78.57, 92.86, 57.14)),
res_df2 = list(value = c("yes", "no"), count = c(4, 10), ratio = c(28.57, 71.43)))
str(result)
List of 2
$ res_df1:List of 3
..$ value: chr [1:3] "phone" "mail" "zoom"
..$ count: num [1:3] 11 13 8
..$ ratio: num [1:3] 78.6 92.9 57.1
$ res_df2:List of 3
..$ value: chr [1:2] "yes" "no"
..$ count: num [1:2] 4 10
..$ ratio: num [1:2] 28.6 71.4
I’ve come up with a solution, that produces this outcome but it feels more like a workaround and not a quite nice R solution:
library(rlist)
result <- list()
for(i in 1:length(summary)){
tmp <- (summary[[i]])
value <- as.character(tmp[seq(1, length(tmp), 3)])
count <- as.numeric(tmp[seq(2, length(tmp), 3)])
ratio <- as.numeric(tmp[seq(3, length(tmp), 3)])
df <- cbind.data.frame(value, count, ratio)
result <- list.append(result, df)
}
I couldn’t come up with a working solution which contains for example a lapply approach or similar. Is there a nicer, more compact way to do this? Any suggestions are appreciated!