2014-10-07 56 views
-1

我想知道是否有人在這裏能幫助我。我有兩個數據幀包含3列:namesgenderamount,兩年不同。我希望能夠比較每個數據框中名稱的起起落落。比較R中兩個數據幀的值

預先感謝您!

編輯 -

d=read.table("names2000.txt",header=FALSE,sep=",") 
colnames(d)=c('name','gender','amount') 

這爲今年2000提供數據。然後我用do.call(rbind, by(d, list(d$gender), FUN=head,5))找到前5名男性和女性的名字,其返回

name gender amount 
F.1  Linda F 80412 
F.2  Mary F 65443 
F.3 Patricia F 47920 
F.4  Barbara F 41560 
F.5  Susan F 38019 
M.6107 James M 86139 
M.6108 Robert M 83534 
M.6109  John M 79396 
M.6110 Michael M 65141 
M.6111 David M 60704 

爲我所用

j=read.table("names2010.txt",header=FALSE,sep=",") 
    colnames(j)=c('name','gender','amount') 

這爲今年「2010」的返回數據的第二數據幀我試圖再次使用do.call(rbind, by(d, list(d$gender), FUN=head,5))發現男性和女性在今年的前5名,但我得到的錯誤信息

> "Error in tapply(seq_len(33983L), list(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, : arguments must have same length" 

我想例如是否比較前10名VALU與2010年數據集相比,2000年數據框的數據增長或下降。有沒有一種方法可以計算出兩年內名稱增加最多的金額?

+0

請發佈一些您的數據樣本,以及您迄今爲止所做的任何嘗試。 [請閱讀這篇文章](http://whathaveyoutried.com) – Barranka 2014-10-07 19:58:55

+0

我已經編輯了上述內容,以包含我的數據的一個小樣本。它返回兩個數據框的表格,每個表格列出了當年出生的嬰兒的名字,按女性的數量從大到小的順序排列,然後是男性。對不起,我沒有其他的東西了。我真的不知道該從哪裏開始:( – Frankj77 2014-10-07 20:27:47

+0

)你的問題現在處於暫停狀態,所以它不能得到任何答案。你認爲你是別人並問自己:「我能幫助這個人嗎?有足夠的信息嗎?「請閱讀:[幫助中心:如何問一個好問題](http://stackoverflow.com/help/How-to-ask)。編輯你的問題並添加一個數據樣本(它不我們很樂意提供幫助,但我們需要很好的信息才能開始 – Barranka 2014-10-07 20:46:49

回答

0

我創建了兩個僞數據幀。如果您可以從下次提供完整的數據集,那將是非常好的。在這裏,我結合了兩個數據遊戲,按年份和性別分列前五名,最後計算出增益(增加/減少)。

library(dplyr) 

df1 <- data.frame(
     names = c("Ana", "Beth", "Caroline", "Diana", "Ellen", 
        "Felicity", "Grace", "Happy", "Irine", "Jasmin", 
        "Andrew", "Bob", "Cabal", "Dan", "Edward", 
        "Fred", "Greg", "Hugh", "Illia", "Jacob"), 
     gender = rep(c("F", "M"), each = 10), 
     frequency = sample.int(1e6, 20, replace = TRUE), 
     year = 2000, 
     stringsAsFactors = FALSE) 


df2 <- data.frame(
     names = c("Ana", "Beth", "Caroline", "Diana", "Ellen", 
        "Felicity", "Grace", "Happy", "Irine", "Jasmin", 
        "Andrew", "Bob", "Cabal", "Dan", "Edward", 
        "Fred", "Greg", "Hugh", "Illia", "Jacob"), 
     gender = rep(c("F", "M"), each = 10), 
     frequency = sample.int(1e6, 20, replace = TRUE), 
     year = 2010, 
     stringsAsFactors = FALSE) 

foo <- rbind(df1,df2)%>% 
     arrange(year, gender, desc(frequency)) %>% # Sort your data by the three columns 
     group_by(year, gender) %>% 
     filter(row_number() < 6) %>% # pick up the top 5 names by year and gender 
     ungroup() %>% 
     group_by(names) %>% 
     mutate(gain = frequency - lag(frequency)) %>% # calculate increase/decrease 
     ungroup() %>% 
     filter(year == 2010) # select top 5 names from 2010, NA means the name was not in top 5 in 2000. 

如果增益爲NA,這意味着如果增益爲正, 的名稱是前5名2000年和2010年獲得流行的名字是沒有進入前5於2000年。如果收益爲負, 這些年名列前五名,但受歡迎程度較低。由於我使用sample.int, ,你的結果會有所不同。

#  names gender year frequency gain 
#1  Ana  F 2010 934706  NA 
#2  Irine  F 2010 869691 240576 
#3 Caroline  F 2010 651674  NA 
#4 Felicity  F 2010 386115 -512275 
#5  Happy  F 2010 382388 -278410 
#6 Edward  M 2010 827374 57532 
#7  Greg  M 2010 794240 76621 
#8  Illia  M 2010 723711  NA 
#9  Fred  M 2010 668467  NA 
#10  Bob  M 2010 599566  NA 
+0

我收到錯誤「錯誤:不正確的大小(1),期待:44284」 – Frankj77 2014-10-08 14:50:10

+0

@ Frankj77我用我的機器再次測試了代碼,它正在工作。你可以做的一件事是挑選代碼。你可以把每一行加起來看看R是否快樂。例如,你可以運行'rbind(df1,df2)%>%arrange(year,gender,desc(frequency))'看看R返回的結果。如果你看到一個數據框,你想添加另一行,看看R是否快樂。順便說一句,我使用dplyr 0.3。 – jazzurro 2014-10-08 15:06:27

+0

它與您的示例數據幀完美協作。我認爲我的規模太大了,因爲當我運行代碼的前兩行時,出現「尺寸不正確(1),期待:44284」的錯誤。有沒有分配內存空間到數據框的方法,也許這會解決問題? – Frankj77 2014-10-08 15:12:06