簡體   English   中英

R合並與row-_and_ colnames匹配的數據幀

[英]R merge data frames matching row- _and_ colnames

我正在尋找一種簡單的方法來組合兩個數據框,將較小的數據框插入較大的數據框中,如下所示:

x.1:                              x.2:
         1.1  1.2  2.3  3.4                 1.2  2.3
    a.b    w              z            d.e    u
    b.c         x                      a.b         v
    d.e    y

期望的結果:

x.f:
         1.1  1.2  2.3  3.4
    a.b    w         v    z
    b.c         x
    d.e    y    u

很多問題僅在cols之后合並,但是我想根據cols rows的名稱進行合並而不是得到它。 名稱中的點是修復方案。 如果有人提出建議會很高興,因為我嘗試合並,加入等等都沒有成功。 我腦海中的一個解決方案是手動方式,循環遍歷較小的框架,每次查找值並保存行/列,然后插入更大的框架。 或重組我的數據幀。 但必須有一個更簡單的方法嗎?

先謝謝,羅賓

這是快速嘗試的代碼:

i<-c("w", "", "y")
j<-c("", "x", "")
k<-c("","","")
l<-c("z","","")
x.1 <- data.frame(i,j,k,l, row.names=c("a.b","b.c","d.e"))
colnames(x.1)<-c("1.1","1.2","2.3","3.4")
m<-c("u", "")
n<-c("", "v")
x.2 <- data.frame(m,n, row.names=c("d.e","a.b"))
colnames(x.2)<-c("1.2","2.3")

這是一種方法:

library(reshape2)
mx1 <- melt(cbind(id = rownames(x.1), x.1), id.vars="id")
mx2 <- melt(cbind(id = rownames(x.2), x.2), id.vars="id")
x12 <- rbind(mx1, mx2)
out <- dcast(x12[!x12$value == "", ], id ~ variable)
out[is.na(out)] <- ""
out
#    id 1.1 1.2 2.3 3.4
# 1 a.b   w       v   z
# 2 b.c       x        
# 3 d.e   y   u       

通過使每個數據集為“長”數據集開始(最簡單的使用melt從“reshape2”),然后將其轉換回一個“寬”的數據集(使用dcast ,再從“reshape2”)。

上述步驟並非都是必需的,但我已將它們包含在盡可能接近您所需的輸出中,以便您可以決定保留/刪除哪些步驟。


實際上,如果你問我,我會停在“x12”階段。 從長遠來看,操作和使用“長”數據可能會更方便(沒有雙關語意)。


更新

您可能還需要考慮“datamerge”包,它實際上包含兩個函數: clean.factors()version.merge clean.factors函數將在合並之前將空白轉換為NA 我保持verbose = TRUE因此您可以看到它確實為您提供了有關如何執行合並的詳細信息,包括是否必須在此過程中更改任何值。

out <- Reduce(function(x, y) version.merge(x, y, add.values = TRUE, verbose = TRUE), 
              lapply(list(x.1, x.2), clean.factors, verbose = FALSE))
# Rows:  3 from `x` #1
#        0 from `y` #2
# 
# Columns:
# 1.1  Origin: `x` #1
# 1.2  Origin: `x` #1
#      Imputed 1 values from `y` #2
# 2.3  Origin: `x` #1
#      Imputed 1 values from `y` #2
#      Class missmatch: numeric vs. character
#      Converted to character
# 3.4  Origin: `x` #1

out
#      1.1  1.2  2.3  3.4
# a.b    w <NA>    v    z
# b.c <NA>    x <NA> <NA>
# d.e    y    u <NA> <NA>

當然,如果你想再次用空格替換NA ,你只需要使用out[is.na(out)] <- ""

flatx.2 <- which(!x.2 =="", arr.ind=TRUE)
flatx.2[] <- cbind( rownames(x.2)[flatx.2[,'row']], 
                    colnames(x.2)[flatx.2[,'col']])
flatx.2  
# contains row and column names in same positions as the non-blank x.2 values
#---------
    row   col  
d.e "d.e" "1.2"
a.b "a.b" "2.3"
#--------------
x.1[ cbind(  match(flatx.2[,1], rownames(x.1)),          #identify numeric row
            match(flatx.2[,2], colnames(x.1))) ] <-      #identify numeric col
                 x.2[which(!x.2 =="", arr.ind=TRUE)]    # the non-blank values
 x.1
#-------------
    1.1 1.2 2.3 3.4
a.b   w       v   z
b.c       x        
d.e   y   u        

我碰巧認為這只是使用基本索引操作(並且應該相當有效並適用於具有所需技能的數據的結構),所以希望有一點掌聲。 我以為我可以在LHS上使用位置的字符值矩陣,但在我的嘗試中出錯了。 ?"["頁面sems說它應該有效,如果我發出語法錯誤,也許這可以簡化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM