Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 以不等长度保存数据,而不引入NA';s_R_Matrix_Save_Dataframe_Nas - Fatal编程技术网

R 以不等长度保存数据,而不引入NA';s

R 以不等长度保存数据,而不引入NA';s,r,matrix,save,dataframe,nas,R,Matrix,Save,Dataframe,Nas,我有一个关于保存长度不等的数据帧的问题。有没有办法在不引入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 = li

我有一个关于保存长度不等的数据帧的问题。有没有办法在不引入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))

x数据帧必须是矩形的。如果要存储可变长度的数据,需要使用列表


您的数据是什么让您希望将其存储在数据框中的?

我会创建一个列表,并使用
write
写入文件。还有其他可能性(请参阅帮助文件以了解
?write


它不必是数据帧,我需要保存一个行不相等的表。这可能吗?@Lisann,你可以将一个列表(每次每个元素)写入一个文件(请参见
?write
,注意
append=TRUE
)。@Roman你能写一个例子吗?因为我不知道你的确切意思。。谢谢你的回答
myl <- list(a = letters[1:10], b = 1:3, c = "kaplah") #create some data

# for every element in the list (`myl`), write that element to a file
# and append if necessary. also, if list element is a character, write
# to as many columns as there are characters.
lapply(X = myl, FUN = function(x) {
    write(x, append = T, file = "test.txt", ncolumns = length(x))
})
a b c d e f g h i j
1 2 3
kaplah