2012-12-31 10 views
10

मैं dat2 से चर जोड़ना चाहते हैं:मर्ज dataframes, अलग-अलग लंबाई

  concreteness familiarity typicality 
amoeba   3.60  1.30  1.71 
bacterium   3.82  3.48  2.13 
leech    5.71  1.83  4.50 

dat1 करने के लिए:

ID variable value 
1 1 amoeba  0 
2 2 amoeba  0 
3 3 amoeba NA 
251 1 bacterium  0 
252 2 bacterium  0 
253 3 bacterium  0 
501 1  leech  1 
502 2  leech  1 
503 3  leech  0 

निम्नलिखित उत्पादन देते:

X ID variable value concreteness familiarity typicality 
1 1 1 amoeba  0   3.60  1.30  1.71 
2 2 2 amoeba  0   3.60  1.30  1.71 
3 3 3 amoeba NA   3.60  1.30  1.71 
4 251 1 bacterium  0   3.82  3.48  2.13 
5 252 2 bacterium  0   3.82  3.48  2.13 
6 253 3 bacterium  0   3.82  3.48  2.13 
7 501 1  leech  1   5.71  1.83  4.50 
8 502 2  leech  1   5.71  1.83  4.50 
9 503 3  leech  0   5.71  1.83  4.50 

आप देख सकते हैं से जानकारीमें कई पंक्तियों पर दोहराई जानी है।

dat3 <- merge(dat1, dat2, by=intersect(dat1$variable(dat1), dat2$row.names(dat2))) 

निम्न त्रुटि Givng:

dat1:

structure(list(ID = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), variable = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("amoeba", "bacterium", 
"leech", "centipede", "lizard", "tapeworm", "head lice", "maggot", 
"ant", "moth", "mosquito", "earthworm", "caterpillar", "scorpion", 
"snail", "spider", "grasshopper", "dust mite", "tarantula", "termite", 
"bat", "wasp", "silkworm"), class = "factor"), value = c(0L, 
0L, NA, 0L, 0L, 0L, 1L, 1L, 0L)), .Names = c("ID", "variable", 
"value"), row.names = c(1L, 2L, 3L, 251L, 252L, 253L, 501L, 502L, 
503L), class = "data.frame") 

dat2

Error in as.vector(y) : attempt to apply non-function 

कृपया यहाँ उदाहरण को दोहराने लगता है

यह मेरी असफल प्रयास किया गया था:

structure(list(concreteness = c(3.6, 3.82, 5.71), familiarity = c(1.3, 
3.48, 1.83), typicality = c(1.71, 2.13, 4.5)), .Names = c("concreteness", 
"familiarity", "typicality"), row.names = c("amoeba", "bacterium", 
"leech"), class = "data.frame") 

उत्तर

12

आप dat2 मर्ज तो उपयोग करने के लिए एक चर में शामिल होने के जोड़ सकते हैं:

dat2$variable <- rownames(dat2) 
merge(dat1, dat2) 
    variable ID value concreteness familiarity typicality 
1 amoeba 1  0   3.60  1.30  1.71 
2 amoeba 2  0   3.60  1.30  1.71 
3 amoeba 3 NA   3.60  1.30  1.71 
4 bacterium 1  0   3.82  3.48  2.13 
5 bacterium 2  0   3.82  3.48  2.13 
6 bacterium 3  0   3.82  3.48  2.13 
7  leech 1  1   5.71  1.83  4.50 
8  leech 2  1   5.71  1.83  4.50 
9  leech 3  0   5.71  1.83  4.50 
+1

इस उत्तर से पता चला नमूना डेटा के साथ काम करता लेकिन यदि कोई था तो 'dat1' में सभी बेजोड़ पंक्तियां छोड़ देंगे। –

+1

@ जी Grothendieck अच्छा पकड़ !! all.x = टी जोड़ने की जरूरत है। – agstudy

7

कुछ भी नहीं @ agstudy के जवाब के साथ कुछ गलत है, लेकिन आप वास्तव में एक गुमनाम अस्थायी बनाने के द्वारा dat2 को संशोधित किए बिना यह कर सकते हैं। जोड़ा जा रहा है एक्स समान है:

> merge(cbind(dat1, X=rownames(dat1)), cbind(dat2, variable=rownames(dat2))) 
    variable ID value X concreteness familiarity typicality 
1 amoeba 1  0 1   3.60  1.30  1.71 
2 amoeba 2  0 2   3.60  1.30  1.71 
3 amoeba 3 NA 3   3.60  1.30  1.71 
4 bacterium 1  0 251   3.82  3.48  2.13 
5 bacterium 2  0 252   3.82  3.48  2.13 
6 bacterium 3  0 253   3.82  3.48  2.13 
7  leech 1  1 501   5.71  1.83  4.50 
8  leech 2  1 502   5.71  1.83  4.50 
9  leech 3  0 503   5.71  1.83  4.50 
8

इस प्रयास करें:

merge(dat1, dat2, by.x = 2, by.y = 0, all.x = TRUE) 

मतलब यह है कि अगर उस बेजोड़ तो परिणाम में dat2 कॉलम NA साथ और अगर भरा जाना चाहिए रहे हैं dat1 में किसी भी पंक्तियों हैं वहाँ dat2 में बेजोड़ मान हैं तो वे अवहेलना कर रहे हैं। उदाहरण के लिए:

dat2a <- dat2 
rownames(2a)[3] <- "elephant" 
# the above still works: 
merge(dat1, dat2a, by.x = 2, by.y = 0, all.x = TRUE) 

ऊपर जाना जाता है के रूप में एक छोड़ दिया एसक्यूएल में में शामिल होने और sqldf में इस तरह किया जा सकता है (चेतावनी को अनदेखा):

library(sqldf) 
sqldf("select * 
     from dat1 left join dat2 
     on dat1.variable = dat2.row_names", 
     row.names = TRUE) 
संबंधित मुद्दे