2017-07-29 68 views
1

首先,我有兩個獨立文本來源的特徵矩陣和data.frame特徵。在每一種情況下,我都執行了不同的文本挖掘方法。現在,我想將它們結合起來,但我知道他們中的一些具有相同名稱的列如下所示:R - 合併/組合具有相同名稱的列但某些數據值等於零

> dtm.matrix[1:10,66:70] 
     cough nasal sputum yellow intermitt 
    1  1  0  0  0   0 
    2  1  0  0  0   0 
    3  0  0  0  0   0 
    4  0  0  0  0   0 
    5  0  0  0  0   0 
    6  1  0  0  0   0 
    7  0  0  0  0   0 
    8  0  0  0  0   0 
    9  0  0  0  0   0 
    10  0  0  0  0   0 

> dim(dtm.matrix) [1] 14300 6543

和第二組是這樣的:

> data1.sub[1:10,c(1,37:40)] 
    Data number cough coughing up blood dehydration dental abscess 
1   1  0     0   0    0 
2   3  1     0   0    0 
3   6  0     0   0    0 
4   8  0     0   0    0 
5   9  0     0   0    0 
6   11  1     0   0    0 
7   12  0     0   0    0 
8   13  0     0   0    0 
9   15  0     0   0    0 
10   16  1     0   0    0 
> dim(data1.sub) 
[1] 14300 168 

我從this topic得到這個代碼,但我是新來的R,我仍然需要一些幫助:

​​

當我運行這個代碼時,它返回一個1x6667的矩陣,並且不會將兩個數據集中的「咳嗽」(或任何其他列)合併在一起。我很困惑。你能幫我解釋這是如何工作的嗎?

+1

好像你只是試圖合併在每個稱爲「數據號」共享列這兩個對象,如果是這樣的話,因爲要保留您的數據的其餘部分,一個簡單的合併將工作,重要的是要將所有在兩個集合中重複的列(將被合併)明確地引用爲關鍵字。爲了實現這一點,兩個集合中所有鍵的所有數據都必須與另一個集合中的相同列和行相同。您應該首先將它們轉換爲相同類型的數據結構,如數據框。 – sconfluentus

+0

我打算將這些對象合併在一起,用於所有不具有相同名稱的列。對於具有相同名稱的列,我想將兩個列合併到一個列中,同時保留兩個對象中的所有= 1值。如果這是明確的,你能告訴我一個有效的代碼示例嗎? – Diana01

+2

歡迎來到StackOverflow!作爲站點策略(甚至在'r'標籤內),我們要求您提供一個最小化,完整,可驗證的示例(MCVE),而不是向我們展示數據的外觀。請提供可重現的數據,例如'dput'或使用您問題中的內置數據集。請參閱http://stackoverflow.com/help/mcve和https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

回答

1

有很多方法可以做到這一點,f.e.使用鹼基R,data.tabledplyr。選擇取決於你的數據量,如果你說,使用非常大的矩陣(這通常是自然語言處理和一袋文字表示的情況),你可能需要用不同的方式來解決你的問題並分析更好(=最快)的解決方案。 我通過dplyr做了你想要的。這有點醜陋,但它起作用。我只是合併兩個數據幀,然後使用for週期來查找這兩個數據幀中存在的變量:將它們相加(variable.x和variable.y),然後刪除em。請注意,我爲了重現性而更改了一些列名,但不應該有任何影響。請讓我知道這是否適合你。

df1 <- read.table(text = 
'  cough nasal sputum yellow intermitt 
1  1  0  0  0   0 
2  1  0  0  0   0 
3  0  0  0  0   0 
4  0  0  0  0   0 
5  0  0  0  0   0 
6  1  0  0  0   0 
7  0  0  0  0   0 
8  0  0  0  0   0 
9  0  0  0  0   0 
10  0  0  0  0   0') 

df2 <- read.table(text = 
' Data_number cough coughing_up_blood dehydration dental_abscess 
1   1  0     0   0    0 
2   3  1     0   0    0 
3   6  0     0   0    0 
4   8  0     0   0    0 
5   9  0     0   0    0 
6   11  1     0   0    0 
7   12  0     0   0    0 
8   13  0     0   0    0 
9   15  0     0   0    0 
10   16  1     0   0    0') 

# Check what variables are common 
common <- intersect(names(df1),names(df2)) 

# Set key IDs for data 
df1$ID <- seq(1,nrow(df1)) 
df2$ID <- seq(1,nrow(df2)) 

# Merge dataframes 
df <- merge(df1, df2,by = "ID") 

# Sum and clean common variables left in merged dataframe 
library(dplyr) 

for (variable in common){ 
    # Create a summed variable 
    df[[variable]] <- df %>% select(starts_with(paste0(variable,"."))) %>% rowSums() 
    # Delete columns with .x and .y suffixes 
    df <- df %>% select(-one_of(c(paste0(variable,".x"), paste0(variable,".y")))) 
} 

df 
    ID nasal sputum yellow intermitt Data_number coughing_up_blood dehydration dental_abscess cough 
1 1  0  0  0   0   1     0   0    0  1 
2 2  0  0  0   0   3     0   0    0  2 
3 3  0  0  0   0   6     0   0    0  0 
4 4  0  0  0   0   8     0   0    0  0 
5 5  0  0  0   0   9     0   0    0  0 
6 6  0  0  0   0   11     0   0    0  2 
7 7  0  0  0   0   12     0   0    0  0 
8 8  0  0  0   0   13     0   0    0  0 
9 9  0  0  0   0   15     0   0    0  0 
10 10  0  0  0   0   16     0   0    0  1 
相關問題