如何在不知道data.frames

时间:2015-12-09 01:53:46

标签: r dataframe cbind

我希望找到一种方法来cbind具有不同长度的data.frames,但是我想要cbind data.frames而不必指定长度。由于我将使用的data.frames的长度会有所不同,我不想在使用cbind之前找到长度。

我收到的当前错误

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 8, 10

所需的输出空白

         A B
rwone    1 2
rwtwo    2 2
rwthree    3

NA的期望输出

         A  B
rwone    1  2
rwtwo    2  2
rwthree  NA 3

我试过了。

length(A) = length(B)

cbind(A,B)

我也试过了。

if(length(A)<length(B)) {
     if(first==1) A = c(rep(NA, length(B)-length(A)),A);B=B
     if(first==0) A = c(A,rep(NA, length(B)-length(A)));B=B
} 

if(length(B)<length(A)) {
     if(first==1) B = c(rep(NA, length(A)-length(B)),B);A=A
     if(first==0) B = c(y,rep(NA, length(A)-length(B)));A=A
} 

cbind(A,B)

非常感谢任何帮助。

德瑞

2 个答案:

答案 0 :(得分:1)

当您合并数据帧的列而不是rownames时,合并是最直接的。如果您将两个数据帧中的列分配为与rownames相同,则merge应该起作用。这对我有用:

Option Explicit

Sub EventsForTomorrow()
'
' EventsForTomorrow Macro
' This macro will fetchevent scheduled for tomorrow data from AIX workbook
'
' Keyboard Shortcut: Ctrl+Shift+E
'
    Workbooks.Open Filename:= _
        "C:\Users\agoel5\Desktop\2015 AIX Integrated Workbook and Schedule.xlsb"
    Range("A11").Select
    Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
    ActiveSheet.ListObjects("Review_Dates").Range.AutoFilter Field:=2, Criteria1 _
        :=xlFilterTomorrow, Operator:=xlFilterDynamic
    Columns("A:BD").Select
    Selection.EntireColumn.Hidden = False
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:E").Select
    Selection.Delete Shift:=xlToLeft
    Columns("F:L").Select
    Selection.Delete Shift:=xlToLeft
    Columns("F:F").Select
    Selection.Delete Shift:=xlToLeft
    Columns("H:I").Select
    Selection.Delete Shift:=xlToLeft
    Columns("J:J").Select
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 12
    ActiveWindow.ScrollColumn = 13
    ActiveWindow.ScrollColumn = 15
    ActiveWindow.ScrollColumn = 14
    ActiveWindow.ScrollColumn = 10
    ActiveWindow.ScrollColumn = 9
    Columns("J:AL").Select
    Selection.Delete Shift:=xlToLeft
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 1
    Range("Review_Dates[[#Headers],[Planned Date]]").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range("A3:I14181").Select
    Selection.Copy
    Application.CutCopyMode = False
    Selection.Copy
    Workbooks.Open Filename:= _
        "C:\Users\agoel5\Desktop\Events for tomorrow format.xlsx"
    Range("B7").Select
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
    Range("G10").Select
End Sub

您还可以使用A = data.frame(A = c(1, 2)) B = data.frame(B = c(2,2,3)) A$key = row.names(A) B$key = row.names(B) merge(A, B, all = TRUE) 库中的join

plyr

答案 1 :(得分:0)

使用基于this post

的合并对Nancy的答案进行小修改
A = data.frame(A = c(1, 2))
B = data.frame(B = c(2,2,3))
A$key = 1:nrow(A)
B$key = 1:nrow(B)

merge(A, B, all = TRUE)