我在r中有两个dataframe,如下所示:
df1 <- data.frame(tracks = rep(c(0:4), each=16), nodes = rep(c("nose", "eyeR", "eyeL", "head", "spine1", "spine2", "caudal", "tail"), length.out=80), coordinates = rep(c("x", "y"), length.out=80))
df2 <- as.data.frame(matrix(runif(n=4000, min=0, max=700), nrow=50))
基本上df 1包含df 2的标签信息。我需要一个输出df,其中我基本上有df 1的第4列,这是df 2的每一列堆叠在彼此的顶部,并且df 1的相应行标签重复,但是df 2有很多行(在这种情况下是50,但在我自己的数据集中要大得多)。任何帮助都将不胜感激!谢谢。
1条答案
按热度按时间cmssoen21#
您可以使用
rep()
函数创建df1
的行的副本,然后使用cbind()
将两者粘在一起。这给出了一个输出,其中前几行和列如下所示:
如果需要,您可以使用
pivot_longer()
进一步重塑数据。