在R中将一个数据帧添加到另一数据帧的末尾 [英] Adding a one dataframe to the end of another data.frame in R

查看:67
本文介绍了在R中将一个数据帧添加到另一数据帧的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将一个添加到另一个数据框的底部。

I'm having difficulty adding one to the bottom of another data frame.

我有一个具有1行和5的数据框(我们称其为DF1)。列。我有另一个具有50行和5列的数据框(我们称其为DF2)。我将其设置为使两个数据帧之间的列都匹配-它们具有相同的列。实际上,DF1是基于DF2的计算。

I have one data frame (let's call it DF1) that has 1 row and 5 columns. I have another dataframe (lets call it DF2) that has 50 rows, and 5 columns. I set it up such that the columns between BOTH data frames match - they have the same columns. In fact, DF1 is a calculation based on DF2.

这就是DF1的样子:

   row.names     pt1     pt2     pt3     pt4
   calc          0.93    0.45    0.28    0.54

这是DF2的样子:

   row.names     pt1     pt2     pt3     pt4
   SNP1          AA      AG      AG      AA       
   SNP2          CT      CT      TC      CC
   SNP3          GG      CG      CG     <NA>
   SNP4          AA      GG      AG      AA
   SNP5         <NA>    <NA>    <NA>    <NA>

DF1应该是实际数据点的数量(不丢失的值数)除以可能值的总数。

DF1 is supposed to be the the number of actual data points(# of values that is not missing) divided by the total number of possible values.

SO ..我想将DF1添加到DF2的底部,看起来像这样:

SO.. I want to add DF1 to the bottom of DF2 to look like this:

   row.names     pt1     pt2     pt3     pt4
   SNP1          AA      AG      AG      AA       
   SNP2          CT      CT      TC      CC
   SNP3          GG      CG      CG     <NA>
   SNP4          AA      GG      AG      AA
   SNP5         <NA>    <NA>    <NA>    <NA>
   calc          0.93    0.45    0.28    0.54

当我尝试使用

 both.dfs <- rbind(DF1, DF2)  # DF1 is first here

DF1是DF2中的第一行。我需要成为最后一行。

DF1 is the first row in DF2. I NEED it to be the LAST row.

当我尝试使用

both.dfs <- rbind(DF2, DF1)  # DF2 is first here

我得到一个错误:

Warning messages:
1: In `[<-.factor`(`*tmp*`, iseq, value = 0.84) :
  invalid factor level, NAs generated
2: In `[<-.factor`(`*tmp*`, iseq, value = 0.84) :
  invalid factor level, NAs generated
3: In `[<-.factor`(`*tmp*`, iseq, value = 0.84) :
  invalid factor level, NAs generated
4: In `[<-.factor`(`*tmp*`, iseq, value = 0.74) :
  invalid factor level, NAs generated

我尝试了合并,我尝试将新行添加到DF2,然后将DF2的值减为零。似乎没有任何效果!我迫切需要一些帮助!任何人?

I've tried merge, I've tried adding a new row to DF2 and then subbing in the values of DF2..nothing seems to work! I'm in desperate need of some help! Anyone?

推荐答案

这是您应该执行的操作:

Here's what you should do:

DFtranspose <- cbind(t(DF1[2, ]), t(DF2))
rownames(DFtranspose) <- DF1[1, ]

这篇关于在R中将一个数据帧添加到另一数据帧的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆