在不引入 NA 的情况下保存不等长度的数据
我有一个关于保存长度不等的数据帧的问题。有没有办法在不引入 NA 之类的情况下保存可变长度的表?这是一个 NA 的示例,但这不是我想要保存的。
x <- list(matrix(c(1,4,3,2), ncol = 2,
dimnames = list(c("A","B"), NULL)),
matrix(c(23,9,4,4,22,54), ncol = 2,
dimnames = list(c("C","D","E"), NULL)))
out <- lapply(x, rownames)
foo <- function(x, max, repl = NA) {
if(length(x) == max)
out <- x
else {
out <- rep(repl, max)
out[seq_along(x)] <- x
}
out
}
out <- lapply(out, foo, max = max(sapply(out, length)))
(out <- do.call(rbind, out))
谢谢
I have a question about saving a dataframe with unequal lengths. Is there way to save table with variable lengths without introducing NA's or something? Here is an example with NA's but that is not what i want to save.
x <- list(matrix(c(1,4,3,2), ncol = 2,
dimnames = list(c("A","B"), NULL)),
matrix(c(23,9,4,4,22,54), ncol = 2,
dimnames = list(c("C","D","E"), NULL)))
out <- lapply(x, rownames)
foo <- function(x, max, repl = NA) {
if(length(x) == max)
out <- x
else {
out <- rep(repl, max)
out[seq_along(x)] <- x
}
out
}
out <- lapply(out, foo, max = max(sapply(out, length)))
(out <- do.call(rbind, out))
Thank you
我将创建一个列表并使用
write
写入文件。还有其他可能性(请参阅帮助文件中的?write
)。结果是
I would create a list and write to a file using
write
. There are other possibilities (see help file for?write
).The result is