使用dplyr基于列值对R中的值求和

时间:2019-02-08 22:40:03

标签: r dataframe dplyr

我有一个包含以下信息的数据集:

=ARRAYFORMULA({"Date", "Name", "Type", "Value"; QUERY({
 QUERY({B3:I, IF(F3:F<>"", $F$2, )},
 "select Col1, Col2, Col9, Col5 where Col9 is not null", 0);
 QUERY({B3:I, IF(G3:G<>"", $G$2, )},
 "select Col1, Col2, Col9, Col6 where Col9 is not null", 0);
 QUERY({B3:I, IF(H3:H<>"", $H$2, )},
 "select Col1, Col2, Col9, Col7 where Col9 is not null", 0);
 QUERY({B3:I, IF(I3:I<>"", $I$2, )},
 "select Col1, Col2, Col9, Col8 where Col9 is not null", 0)},
 "select * order by Col1 asc", 0)})

如果UniqueNumber的值> 0,我想对从第1行到UniqueNumber的每个主题的dplyr值求和,并计算平均值。因此,对于主题001,总和= 2,平均值= .67。

Subject    Value1    Value2    Value3      UniqueNumber
001        1         0         1           3
002        0         1         1           2
003        1         1         1           1

编辑:我只是想对“ UniqueNumber”列中列出的列数求和。因此,这遍历每一行,并在“ UniqueNumber”中列出的列处停止。 示例:具有主题002的第2行应将“ Value1”和“ Value2”列中的值相加,而具有主题003的第3行应仅将“ Value1”列中的值相加。

6 个答案:

答案 0 :(得分:9)

不是整洁的爱好者/专家,但我会尝试使用长格式。然后,只需按组的行索引进行过滤,然后在单个列上运行您想要的任何功能(这样更容易)。

library(tidyr)
library(dplyr)

Data %>% 
  gather(variable, value, -Subject, -UniqueNumber) %>% # long format
  group_by(Subject) %>% # group by Subject in order to get row counts
  filter(row_number() <= UniqueNumber) %>% # filter by row index
  summarise(Mean = mean(value), Total = sum(value)) %>% # do the calculations
  ungroup() 

## A tibble: 3 x 3
#  Subject  Mean Total
#     <int> <dbl> <int>
# 1       1 0.667     2
# 2       2 0.5       1
# 3       3 1         1

一种非常类似的方法可以通过列名中的整数进行过滤。过滤器步骤位于group_by之前,因此它有可能提高性能(或没有提高?),但它的鲁棒性较弱,因为我假设感兴趣的列被称为"Value#"

Data %>% 
  gather(variable, value, -Subject, -UniqueNumber) %>% #long format
  filter(as.numeric(gsub("Value", "", variable, fixed = TRUE)) <= UniqueNumber) %>% #filter
  group_by(Subject) %>% # group by Subject
  summarise(Mean = mean(value), Total = sum(value)) %>% # do the calculations
  ungroup()

## A tibble: 3 x 3
#  Subject  Mean Total
#     <int> <dbl> <int>
# 1       1 0.667     2
# 2       2 0.5       1
# 3       3 1         1

只是为了好玩,添加一个data.table解决方案

library(data.table)

data.table(Data) %>% 
  melt(id = c("Subject", "UniqueNumber")) %>%
  .[as.numeric(gsub("Value", "", variable, fixed = TRUE)) <= UniqueNumber,
    .(Mean = round(mean(value), 3), Total = sum(value)),
    by = Subject]

#    Subject  Mean Total
# 1:       1 0.667     2
# 2:       2 0.500     1
# 3:       3 1.000     1

答案 1 :(得分:2)

检查此解决方案:

df %>%
  gather(key, val, Value1:Value3) %>%
  group_by(Subject) %>%
  mutate(
    Sum = sum(val[c(1:(UniqueNumber[1]))]),
    Mean = mean(val[c(1:(UniqueNumber[1]))]),
  ) %>%
  spread(key, val)

输出:

 Subject UniqueNumber   Sum  Mean Value1 Value2 Value3
  <chr>          <int> <dbl> <dbl>  <dbl>  <dbl>  <dbl>
1 001                3     2 0.667      1      0      1
2 002                2     1 0.5        0      1      1
3 003                1     1 1          1      1      1

答案 2 :(得分:1)

使用purrr::map_df(与dplyr来自同一作者的解决方案)。

library(dplyr)
library(purrr)
l_dat <- split(dat, dat$Subject) # first we need to split in a list

map_df(l_dat, function(x) {
  n_cols <- x$UniqueNumber # finds the number of columns
  x <- as.numeric(x[2:(n_cols+1)]) # subsets x and converts to numeric
  mean(x, na.rm=T) # mean to be returned
})
# output:
# # A tibble: 1 x 3
#     `1`   `2`   `3`
#   <dbl> <dbl> <dbl>
# 1 0.667   0.5     1

另一种选择(输出格式更接近dplyr解决方案):

map_df(l_dat, function(x) {
  n_cols <- x$UniqueNumber
  id <- x$Subject
  x <- as.numeric(x[2:(n_cols+1)])
  tibble(id=id, mean_values=mean(x, na.rm=T))
})
# # A tibble: 3 x 2
# id mean_values
# <int>       <dbl>
# 1     1       0.667
# 2     2       0.5  
# 3     3       1   

仅作为示例,我添加了sum()然后除以length(x)-1

map_df(l_dat, function(x) {
  n_cols <- x$UniqueNumber
  id <- x$Subject
  x <- as.numeric(x[2:(n_cols+1)])
  tibble(id=id, 
                mean_values=sum(x, na.rm=T)/(length(x)-1)) # change here
})
# # A tibble: 3 x 2
# id mean_values
# <int>       <dbl>
# 1     1          1.
# 2     2          1.
# 3     3        Inf  #beware of this case where you end up dividing by 0

数据:

tt <- "Subject    Value1    Value2    Value3      UniqueNumber
001        1         0         1           3
002        0         1         1           2
003        1         1         1           1"

dat <- read.table(text=tt, header=T)

答案 3 :(得分:1)

OP可能仅对dplyr解决方案感兴趣,但出于比较目的和将来的读者使用mapply的基本R选项

cols <- grep("^Value", names(df))

cbind(df, t(mapply(function(x, y) {
      if (y > 0) {
        vals = as.numeric(df[x, cols[1:y]])
        c(Sum = sum(vals, na.rm = TRUE), Mean = mean(vals, na.rm = TRUE))
       }
       else 
        c(0, 0)
},1:nrow(df), df$UniqueNumber)))

#  Subject Value1 Value2 Value3 UniqueNumber Sum  Mean
#1       1      1      0      1            3   2 0.667
#2       2      0      1      1            2   1 0.500
#3       3      1      1      1            1   1 1.000

这里,我们根据各行各自的UniqueNumber子集各行,然后如果sum的值大于0或仅返回0,则计算其为meanUniqueNumber

答案 4 :(得分:1)

这是使用tidyr::nestValues列收集到列表中的另一种方法,以便我们可以使用map2遍历表。在每一行中,我们从Values列表列中选择正确的值,并分别取总和或均值。

library(tidyverse)
tbl <- read_table2(
"Subject    Value1    Value2    Value3      UniqueNumber
001        1         0         1           3
002        0         1         1           2
003        1         1         1           1"
)
tbl %>%
  filter(UniqueNumber > 0) %>%
  nest(starts_with("Value"), .key = "Values") %>%
  mutate(
    sum = map2_dbl(UniqueNumber, Values, ~ sum(.y[1:.x], na.rm = TRUE)),
    mean = map2_dbl(UniqueNumber, Values, ~ mean(as.numeric(.y[1:.x], na.rm = TRUE))),
  )
#> # A tibble: 3 x 5
#>   Subject UniqueNumber Values             sum  mean
#>   <chr>          <dbl> <list>           <dbl> <dbl>
#> 1 001                3 <tibble [1 × 3]>     2 0.667
#> 2 002                2 <tibble [1 × 3]>     1 0.5  
#> 3 003                1 <tibble [1 × 3]>     1 1

reprex package(v0.2.1)于2019-02-14创建

答案 5 :(得分:1)

我认为最简单的方法是将实际上应该为NA的零设置为NA,然后在相应的列子集上使用rowSumsrowMeans。 / p>

Data[2:4][(col(dat[2:4])>dat[[5]])] <- NA
Data
#   Subject Value1 Value2 Value3 UniqueNumber
# 1       1      1      0      1            3
# 2       2      0      1     NA            2
# 3       3      1     NA     NA            1

library(dplyr)
Data%>%
  mutate(sum  =  rowSums(.[2:4], na.rm = TRUE),
         mean = rowMeans(.[2:4], na.rm = TRUE))

#   Subject Value1 Value2 Value3 UniqueNumber sum      mean
# 1       1      1      0      1            3   2 0.6666667
# 2       2      0      1     NA            2   1 0.5000000
# 3       3      1     NA     NA            1   1 1.0000000

transform(Data, sum = rowSums(Data[2:4],na.rm = TRUE), mean = rowMeans(Data[2:4],na.rm = TRUE))留在基数R中。

数据

Data <- structure(
  list(Subject = 1:3, 
       Value1 = c(1L, 0L, 1L), 
       Value2 = c(0L, 1L, NA), 
       Value3 = c(1L, NA, NA), 
       UniqueNumber = c(3L, 2L, 1L)), 
  .Names = c("Subject","Value1", "Value2", "Value3", "UniqueNumber"),
  row.names = c(NA, 3L), class = "data.frame")