我想将两个数据表组合起来,其中一个数据表以整洁的格式提供了另一个数据表的信息。我想要的一个例子是合并两个像这样的表:
information <- data.frame(c("standard-1", "standard-2", "standard-3"), c("sample-1", "sample-2", "sample-3"))
data <- data.frame(c(2, 4, 5), c(4, 2, 6))并获得一个新的数据表,其中每一列将来自一个表,每一行都将是一个观察。就像用这个代码创建的那样:
all_data <- data.frame(c("standard-1", "standard-2", "standard-3", "sample-1", "sample-2", "sample-3"), c(2, 4, 5, 4, 2, 6))我尝试了一种非常低劣的方法:为每个表的每一列应用一个循环,并将信息添加到一个新表中,但我总是收到关于具有不同名称的信息(我不关心列或行名称)或大小不同的错误。
如能提供任何帮助,将不胜感激。
发布于 2022-03-11 11:46:15
您可以在向量中同时使用unlist来获得结果数据。
result <- data.frame(info_col = unlist(information),
data_col = unlist(data), row.names = NULL)
result
# info_col data_col
#1 standard-1 2
#2 standard-2 4
#3 standard-3 5
#4 sample-1 4
#5 sample-2 2
#6 sample-3 6这将考虑到两个数据都具有相同的维数,并且在向量中未列出时具有相同的长度。
https://stackoverflow.com/questions/71437906
复制相似问题