4

我正在尝试学习简化我的代码并将多个data.frames(> 2)同时合并到一个数据集中。首先,我想为四个 PCA 列( 、、 ...)中的每一个计算“站点” meansdn(每个站点的“个人”数量)。其次,将结果合并到单个 data.frame 中。以下是我尝试执行此任务的示例数据和代码。Morph_PC1Morph_PC2

我意识到可能有一种方法可以生成不需要合并的单个数据集,这会很棒,但我也想知道如何使merge_all包中的命令reshape起作用。

样本数据:

WW_Data <- structure(list(Individual_ID = c("WW_00A_05", "WW_00A_03", "WW_00A_02", 
"WW_00A_01", "WW_00A_04", "WW_00A_06", "WW_00A_08", "WW_00A_09", 
"WW_00A_07", "WW_00A_10", "WW_09AB_14", "WW_09AB_09", "WW_09AB_13", 
"WW_10AD_01", "WW_10AD_09", "WW_10AD_04", "WW_10AD_02", "WW_10AD_03", 
"WW_10AD_07", "WW_10AD_08"), Site_Name = c("Alnön", "Alnön", 
"Alnön", "Alnön", "Alnön", "Alnön", "Alnön", "Alnön", "Alnön", 
"Alnön", "Anjan", "Anjan", "Anjan", "Anjan", "Anjan", "Anjan", 
"Anjan", "Anjan", "Anjan", "Anjan"), Morph_PC1 = c(-2.08424433316496, 
-1.85413711191957, -1.67227075271696, -1.0486265729884, -0.809415702756541, 
-2.81781338129716, -2.08471369525797, -0.183840575363918, -0.753930407169699, 
0.0719252507535882, 1.02353521593315, 1.34441686821234, 0.755249445355964, 
-0.564426004755035, 0.720689649641627, -0.243471506156601, -0.245437522679261, 
-0.69936850894502, 0.9160796809062, 2.2881261039382), Morph_PC2 = c(1.28499189140338, 
-0.349487815669147, 0.0148183164519594, -1.55929148726881, -0.681590397005219, 
1.21595114750227, 0.116028310510466, 0.187613229042593, -0.923592436104444, 
-1.50956083294446, 1.44864057855388, 1.46254159976068, 1.20375736157205, 
0.174071006609975, -0.722049893415186, 1.03516327411773, 0.808851776990861, 
-0.928263134752596, -0.175511637463994, -0.389421342417043), 
    Morph_PC3 = c(-0.445087364125436, -0.704903876393893, 0.161983939922481, 
    1.14604411022773, 0.701508422965674, -0.78133408496171, -0.306619974141955, 
    1.05643337302175, 0.163868647932456, -0.673344807228353, 
    -0.337986608605208, -1.01911125040091, 0.258004835638601, 
    -0.648040419259003, -0.196770002944659, 0.614010430132367, 
    0.755886614924319, -0.0631407344114064, -1.28178468134549, 
    0.226362214551239), Morph_PC4 = c(0.0476276463048772, 0.342957387676778, 
    -0.117383887482525, 0.289881853573214, 0.649579005842321, 
    0.600433718752986, 0.295294947111845, -0.293754065807853, 
    -0.43805381119461, 0.520363554131325, -0.393329204345947, 
    -1.05629143416274, -0.370922397397109, 0.115121369773473, 
    0.91445926597504, 0.280048079793911, -0.802245210297552, 
    0.00368405602889952, -0.251898295768711, -0.607995193037228
    )), .Names = c("Individual_ID", "Site_Name", "Morph_PC1", 
"Morph_PC2", "Morph_PC3", "Morph_PC4"), row.names = c(36L, 37L, 
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 137L, 138L, 139L, 140L, 
141L, 142L, 143L, 144L, 145L, 146L), class = "data.frame")

编码:

## Calculate statistics for each site ##
WW_PC1_Mean <- subset(melt(tapply(WW_Data$Morph_PC1,list(WW_Data$Site_Name),mean)), value != FALSE)
WW_PC1_SD <- subset(melt(tapply(WW_Data$Morph_PC1,list(WW_Data$Site_Name),sd)), value != FALSE)
WW_PC2_Mean <- subset(melt(tapply(WW_Data$Morph_PC2,list(WW_Data$Site_Name),mean)), value != FALSE)
WW_Site_SD <- subset(melt(tapply(WW_Data$Morph_PC2,list(WW_Data$Site_Name),sd)), value != FALSE)

## merge the all the datasets with one command - THIS FAILS!
WW_Stats <- merge_all(WW_Site_PC1_Mean, WW_Site_PC1_SD, WW_Site_PC2_Mean, by = c("indices"))

编辑:现在我有一个很好的结果,可以快速将摘要统计信息放入三个文件中,但我仍然在尝试merge_all(尽管我不确定是否应该使用merge_recurse- 无论我得到相同的错误)结果时遇到问题。这是我的尝试:

## Calculate statistics for each site ##
WW_Site_PC_Mean <- ddply(WW_Data, .(Site_Name), numcolwise(mean))
colnames(WW_Site_PC_Mean) <- c("Site_Name", "PC1_Mean", "PC2_Mean", "PC3_Mean", "PC4_Mean")
WW_Site_PC_SD <- ddply(WW_Data, .(Site_Name), numcolwise(sd))
colnames(WW_Site_PC_Mean) <- c("Site_Name", "PC1_SD", "PC2_SD", "PC3_SD", "PC4_SD")
WW_Site_PC_N <- count(WW_Data$Site_Name)
colnames(WW_Site_PC_N) <- c("Site_Name", "PCA_N")


## merge the all the datasets with one command - THIS FAILS!
WW_Stats <- merge_recurse(WW_Site_PC_Mean, WW_Site_PC_SD, WW_Site_PC_N, by = "Site_Name")

错误输出:

Error in fix.by(by.x, x) : 
  'by' must specify column(s) as numbers, names or logical
4

3 回答 3

9

留在基础 R 中,您可以使用aggregate

WW_Data_mean = aggregate(list(mean = WW_Data[, -c(1, 2)]), 
                         list(Site_Name = WW_Data$Site_Name), mean)
WW_Data_sd = aggregate(list(mean = WW_Data[, -c(1, 2)]), 
                       list(Site_Name = WW_Data$Site_Name), sd)

更新(你问题的第二部分)

您的代码有几个错误,也许您需要更多地“玩”合并。

首先,错误。您的示例中失败的行失败,因为:

  1. 它的结构不正确;要合并的data.frames 应该在 a 中list
  2. 它引用了您的示例中不存在的对象!您正在尝试合并一个名为的对象WW_Site_Name_PC1_Mean,但该对象的名称是WW_PC1_Mean.

其次,这里有一些其他的尝试。修正你的列名:

# Fix your column names
# There's probably an easier way to do this, but...
names(WW_PC1_Mean)[2] = "WW_PC1_Mean"
names(WW_PC1_SD)[2] = "WW_PC1_SD"
names(WW_PC2_Mean)[2] = "WW_PC2_Mean"
names(WW_Site_SD)[2] = "WW_Site_SD"

现在,试试merge_all。请注意,您需要提供一个listof data.frames。似乎总是merge_all给出两列——但也许我做错了什么。

# Not what you want
merge_all(list(WW_PC1_Mean, WW_PC1_SD, 
               WW_PC2_Mean, WW_Site_SD), by="indices")
  indices WW_PC1_Mean
1   Alnön  -1.3237067
2   Anjan   0.5295393

继续前进merge_recurse。这有效:

# This is what you want
merge_recurse(list(WW_PC1_Mean, WW_PC1_SD, 
                   WW_PC2_Mean, WW_Site_SD), by="indices")
  indices WW_PC1_Mean WW_PC1_SD WW_PC2_Mean WW_Site_SD
1   Alnön  -1.3237067 0.9252417   -0.220412  0.9912227
2   Anjan   0.5295393 0.9511800    0.391778  0.9112450

您也可以Reduce在基础 R 中使用。

# Base R also has a solution
Reduce(function(x, y) merge(x, y, all=TRUE), 
       list(WW_PC1_Mean, WW_PC1_SD, WW_PC2_Mean, WW_Site_SD))
于 2012-08-08T08:04:40.523 回答
8

我建议你把精力集中在学习一些plyr优点上。

使用该功能ddply,您可以真正简化您的代码。以下是mean使用一行代码计算数据中所有列的方法:

library(plyr)
ddply(WW_Data, .(Site_Name), numcolwise(mean))
  Site_Name  Morph_PC1 Morph_PC2   Morph_PC3  Morph_PC4
1     Alnön -1.3237067 -0.220412  0.03185484  0.1896946
2     Anjan  0.5295393  0.391778 -0.16925696 -0.2169369

同样,标准差:

ddply(WW_Data, .(Site_Name), numcolwise(sd))
  Site_Name Morph_PC1 Morph_PC2 Morph_PC3 Morph_PC4
1     Alnön 0.9252417 0.9912227 0.7316201 0.3766064
2     Anjan 0.9511800 0.9112450 0.6698389 0.5717482

我经常使用这种类型的分析。使用这种策略,我几乎不需要同时合并多个数据帧。

PS。包reshape是旧的 - 您应该使用reshape2它,它不再包含该merge_all()功能

于 2012-08-08T07:57:06.930 回答
0

一些使用 plyr 和信息变量名称的解决方案。

ms <- function(x) cbind("mean"=mean(x),"sd"=sd(x))
do.call(rbind,dlply(WW_Data, .(Site_Name), function(dat) numcolwise(ms)(dat)))



      Morph_PC1.mean Morph_PC1.sd Morph_PC2.mean Morph_PC2.sd Morph_PC3.mean Morph_PC3.sd Morph_PC4.mean Morph_PC4.sd
Alnön     -1.3237067    0.9252417     -0.2204120    0.9912227     0.03185484   0.73162007      0.1896946    0.3766064
Anjan      0.5295393    0.9511800      0.3917780    0.9112450    -0.16925696   0.66983885     -0.2169369    0.5717482
于 2012-08-08T17:38:38.320 回答