合并R中的多个数据帧

时间:2021-01-08 12:47:24

So I have many data frames and I'm trying to merge them. Some of them are in the form:

所以我有很多数据框,我正在尝试合并它们。其中一些形式如下:

    sites1  AA1 SA
1   13: C   0.360828
2   14: S   0.017064
3   15: I   0.010810

Others are:

    sites2  AA2 Freq
1   1:  X   0.013
2   1:  S   0.987
3   2:  L   1.000

I have another data frame linking the proper data frame from the first set with the one from the second set and it goes like this:

我有另一个数据框链接第一组中的正确数据帧和第二组中的数据帧,它是这样的:

    V1  V2
1   1JH6    AT4G18930
2   3MXZ    AT2G30410

with the name on the left side corresponding to one data frame and the name on the right side corresponding to another data frame. I'm trying to merge them by doing

左侧的名称对应一个数据帧,右侧的名称对应另一个数据帧。我正试图通过这样做来合并它们

for (i in 1:n){
  name = paste("1",names2[i,2])
  assign(name,merge(names2[i,1],names2[i,2]))
}

but this just returns a data frame with the two names.. Any help?

但这只返回一个带有两个名字的数据框..任何帮助?

1 个解决方案

#1


1  

try replacing the assign statement inside your for loop with the following

尝试使用以下内容替换for循环中的assign语句

     assign(name,merge(get(as.character(names2[i,1])), 
                       get(as.character(names2[i,2]))))

Also, consider fixing the name = paste.... statement as follows:

另外,请考虑修复name = paste ....语句,如下所示:

   name = paste("T1",names2[i,2], sep="")
   # added sep="" to not have a space.
   # changed the name so that does not start with a number

#1


1  

try replacing the assign statement inside your for loop with the following

尝试使用以下内容替换for循环中的assign语句

     assign(name,merge(get(as.character(names2[i,1])), 
                       get(as.character(names2[i,2]))))

Also, consider fixing the name = paste.... statement as follows:

另外,请考虑修复name = paste ....语句,如下所示:

   name = paste("T1",names2[i,2], sep="")
   # added sep="" to not have a space.
   # changed the name so that does not start with a number