在 R 中总结基于数据的列

Summing data base colums in R

我开始使用 R,很可能我的问题很简单,但尽管如此,我还是花了很多时间试图找出我做错了什么但无济于事。

我必须感谢你,因为我上周在搜索其他问题时发现了这个网站。但是现在作为一个新人,往往很难理解别人的代码。

我的 RStudio 版本是:1.1.442

我的问题是我有两个数据框,一个有一些年份,一个有一些在几个拖网中发现的项目,我需要总结项目并制作另一个变量。其中出现每年的项目汇总和拖网。

所以,我用 for 做了一个循环,得到相同的底部拖网和同一年,以便对项目求和。

我简化了我的数据库。

BT<- c(1, 1, 2, 2, 2, 3, 3, 3, 3, 3)
YEAR<- c(2007, 2007, 2008, 2008, 2008, 2009, 2009, 2009, 2009, 2009)
W<- c(95, 6, 60, 50, 4, 21, 56, 44, 23, 4) 
Data1= data.frame(BT,YEAR,W)

Trawl<- c(1, 2, 3)
Year<- c(2007, 2008, 2009)
Data2= data.frame(Trawl,Year)
peso=cbind()

for(i in 1:length(Data1$BT)) {
  ind=which(Data2$Trawl == Data1$BT[i] & Data2$Year == Data1$YEAR[i])

  print(i)
  print(ind)
  print(Data1$W[ind])
  peso[i]=Data1$W[ind]
  sumaGr[i]=colSums(peso[i])
}

我明白了:

Error in colSums(peso[i]) : 'x' must be an array of at least two dimensions

但我不知道如何修复它。 我将不胜感激您的所有帮助和建议。 先感谢您。

您似乎在执行一些拆分-应用-组合计算。您可以通过以下几种方式完成此操作。

基础 R

Data3 <- aggregate(Data1$W, by = list(Data1$BT, Data1$YEAR), sum)
colnames(Data3) <- c("Trawl", "YEAR", "sumaGr")
Data3

dplyr

Data3 <- Data1 %>%
  group_by(BT, YEAR) %>%
  summarise(sumaGr = sum(W)) %>%
  rename(Trawl = BT)
Data3

data.table

library(data.table)
Data3 <- setDT(Data1)[,.(sumaGr = sum(W)), by = .(BT, YEAR)]
setnames(Data2, "BT", "Trawl")
Data3

这是基础 R 解决方案的输出:

#   Trawl YEAR sumaGr
# 1     1 2007    101
# 2     2 2008    114
# 3     3 2009    148
if(!require(dplyr)) {
  install.packages("dplyr")
  require(dplyr) 
} # for 'inner_join()' install and/or load package dplyr


# Rename for fusion of the two data frames
colnames(Data1) <- c("BT", "Year", "W")
# colnames for 'By=' must look the same!

data1.new <- inner_join(Data1, Data2, by="Year")

# inspect data1.new
data1.new

# split by "Trawl"
df.list <- split(data1.new, data1.new$Trawl)

# summarize each of the data frames in this list
summaries.list <- lapply(df.list, summary)

# But I think what youw ant is colMeans, colSums etc.
colMeans.list <- lapply(df.list, colMeans)
colSums.list  <- lapply(df.list, colSums)

# colMeans(df) is acatually function(df) {apply(df, 2, FUN=mean)}
# in this way you can use any variadic function to make it
# applicable to a whole column (variadic functions are those
# which can take any number of arguments).
# if there is a non-variadic function - let's say max():
# let's say
# max() takes only two arguments (that's not true ...)
# but let's assume it takes only two arguments, then 
# function(your.vector) Reduce(max, your.vector) makes it variadic
# e.g. maximum of a column:
colMax <- function(df) {apply(df, 2, FUN=function(vec) Reduce(max, vec))}
colMax.list   <- lapply(df.list, colMax)

# inspect those objects
colMeans.list
colSums.list
colMax.list

# you can reduce the results using Reduce(rbind, ...)
means.by.trawl.mat <- Reduce(rbind, colMeans.list)
sums.by.trawl.mat  <- Reduce(rbind, colSums.list)
maxs.by.trawl.mat  <- Reduce(rbind, colMax.list)

# give rownames
rownames(means.by.trawl.mat) <- means.by.trawl.mat[, "BT"]
rownames(sums.by.trawl.mat)  <- sums.by.trawl.mat[, "BT"]
rownames(maxs.by.trawl.mat)  <- maxs.by.trawl.mat[, "BT"]

# result
> means.by.trawl.mat
  BT Year    W Trawl
1  1 2007 50.5     1
2  2 2008 38.0     2
3  3 2009 29.6     3
> sums.by.trawl.mat
   BT  Year   W Trawl
2   2  4014 101     2
6   6  6024 114     6
15 15 10045 148    15
> maxs.by.trawl.mat
  BT Year  W Trawl
1  1 2007 95     1
2  2 2008 60     2
3  3 2009 56     3