我有一个小标题如下:
uuu <- structure(list(IsCharacter = c("a", "b"),
ShouldBeCharacter = list("One", "Another"),
IsList = list("Element1", c("Element2", "Element3"))
),
.Names = c("IsCharacter", "ShouldBeCharacter", "IsList"),
row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"))
uuu
## A tibble: 2 × 3
# IsCharacter ShouldBeCharacter IsList
# <chr> <list> <list>
#1 a <chr [1]> <chr [1]>
#2 b <chr [1]> <chr [2]>
我想将“ShouldBeCharacter”之类的列转换为所有元素都具有相同长度和类型的列,类似于“IsCharacter”,其余列保持不变。
到目前为止,我有以下功能可以解决这个问题,但对我来说它看起来很hacky。我想知道是否有更好的解决方案我没有考虑:
lists_to_atomic <- function(data) {
# Elements of length larger than one should be kept as lists.
# So we compute the maximum length for each column
length_column_elements <- apply(data, 2,
function(x) max(sapply(x, function(y) length(y))))
# to_simplify will contain column names of class list and with all elements of length 1
to_simplify <- colnames(data)[length_column_elements == 1 & sapply(data, class) == "list"]
# Do the conversion
data[,to_simplify] <- tibble::as_tibble(lapply(as.list(data[,to_simplify]), function(x) {do.call(c, x)}))
return(data)
}
这是我得到的结果,注意 ShouldBeCharacter 的类型是如何改变的:
lists_to_atomic(uuu)
## A tibble: 2 × 3
# IsCharacter ShouldBeCharacter IsList
# <chr> <chr> <list>
#1 a One <chr [1]>
#2 b Another <chr [2]>
这as_tibble(lapply(as.list(... do.call(c,...)))
条线对我来说看起来太复杂了,但我找不到更简单的替代方案。
是否有任何简化使我的lists_to_atomic
功能更可靠?
更新
我没有考虑tidyr::unnest
在列表类型的列和长度为 1 的元素上使用,但是按照@taavi-p 的回答,我已经能够将函数简化为:
lists_to_atomic <- function(data) {
# Elements of length larger than one should be kept as lists.
# So we compute the maximum length for each column
length_column_elements <- apply(data, 2,
function(x) max(sapply(x, function(y) length(y))))
# to_simplify will contain column names of class list and with all elements of length 1
to_simplify <- colnames(data)[length_column_elements == 1 &
vapply(data,
FUN = function(x) "list" %in% class(x),
FUN.VALUE = logical(1))]
# Do the conversion
data2 <- tidyr::unnest_(data, unnest_cols = to_simplify)
data2 <- data2[, colnames(data)] # Preserve original column order
return(data2)
}