[英]R - Transform Data.frame elements into a single column tibble()
I am looking for a way to improve my code and move away from loops in R.我正在寻找一种方法来改进我的代码并摆脱 R 中的循环。
Background:背景:
I have a list of Data.frames.我有一个 Data.frames 列表。 Each Data.frame has 4 elements - many of them are NULL.
每个 Data.frame 有 4 个元素 - 其中许多是 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'.
我需要保留 NULL 并用 NA 替换它们,因为我征募/展平列表,然后将其作为列添加到“tibble”。 I know if I access the elements using
map(list, n)
it will give me the start point for the loop.我知道如果我使用
map(list, n)
访问元素map(list, n)
它将为我提供循环的起点。 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.如果我
flatten()
这会删除 NULL 对象,我将无法将它添加到我正在使用的另一个 tibble 中,因为订单没有保留。
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.预期的输出是一列小标题,它是列表的长度,为原始列表中的 NULL 元素保留
NA
。
# 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.我知道循环很慢,但我找不到另一种方法来访问 Data.frame 中的元素,然后将其转换为一个可用的(平面)列,以作为每个观察的另一个元素添加到 tibble 中。
Any advice would be greatly appreciated.任何建议将不胜感激。
Thanks谢谢
James詹姆士
Base R option using sapply
-使用
sapply
基础 R 选项 -
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.