I am looking for a way to improve my code and move away from loops in R.
Background:
I have a list of Data.frames. Each Data.frame has 4 elements - many of them are NULL. I need to preserve the NULLs and replace them with an NA as I enlist / flatten the the list to then add it as a column to a 'tibble'. I know if I access the elements using map(list, n)
it will give me the start point for the loop. If I flatten()
this it removes the NULL objects and I won't be able to add it to another tibble I am using as the order isn't preserved.
Input structure is below:
[[1]]
item1 item2 item3 item4
1 aaaa bbbb cccc dddd
[[2]]
data frame with 0 columns and 0 rows
[[3]]
data frame with 0 columns and 0 rows
[[4]]
item1 item2 item3 item4
1 ffff gggg hhhh kkkk
Solution so far:
I have written the following loop:
element_tibble = tibble()
for (i in 1:length(list_of_dfs)){
element = list_of_dfs[[i]]
if (is.null(element) == TRUE) {
element = NA
}
else {
element = element
}
row = c(element = element)
element_tibble = rbind(element_tibble, row)
}
The intended output is a one column tibble that is the length of the list with NA
preserved for NULL elements in the original list.
# A tibble: n x 1
element
<chr>
1 item2
2 NA
3 NA
4 item2
etc
I know the loop is slow but I can't find another way to access the element in the Data.frame then transform it into a usable(flat) column for addition to a tibble as another element for each observation.
Any advice would be greatly appreciated.
Thanks
James
Base R option using sapply
-
df <- data.frame(item1 = 'aaaa', item2 = 'bbbb', item3 = 'ccc', item4 = 'ddd')
list_of_dfs <- list(df, data.frame(), df)
result <- data.frame(element = sapply(list_of_dfs, function(x)
if(nrow(x)) x[[2]] else NA))
result
# element
#1 bbbb
#2 <NA>
#3 bbbb
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.