基于其他列R中的多个标准平均列值

时间:2018-01-09 14:01:09

标签: r tidyverse

my.df1data.frame,具有许多独特的观察结果,但具有相似的特征(在此示例中为ColourType& Size)。对于my.df2中的每个特征组合,我想计算mean中符合条件的所有观察值的SDmy.df1。因此,例如在my.df2的第一行中,我想计算具有以下特征的来自mean的所有观察值的PriceOne和PriceTwo的SDmy.df1:颜色为蓝色,类型1和大小S.注意:对于第5行,我想计算PriceOne的meanSD以及来自my.df1的所有具有蓝色的观察值的PriceTwo,所以不管它们的类型和大小。我的原始数据集有更多的观察,标准变量和价格列,因此高度赞赏可扩展的解决方案。

    my.df1 <- data.frame(Colour = c('Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Red','Red','Red','Red','Red','Red','Red','Red','Red','Red','Red','Red'),
                         Type = c(1,1,2,2,1,2,1,1,2,2,1,2,1,1,2,2,1,2,1,1,2,2,1,2),
                         Size = c('S','S','S','S','S','S','M','M','M','M','M','M','S','S','S','S','S','S','M','M','M','M','M','M'),
                         PriceOne = c(10,15,20,18,19,11,12,16,20,21,10,11,10,15,10,18,20,14,21,15,28,19,10,11),
                         PriceTwo = c(10,15,10,18,20,14,21,15,28,19,10,11,10,15,20,18,19,11,12,16,20,21,10,11))

    my.df1(head)
                     Colour Type Size PriceOne PriceTwo
                1    Blue    1    S       10       10
                2    Blue    1    S       15       15
                3    Blue    2    S       20       10
                4    Blue    2    S       18       18
                5    Blue    1    S       19       20

my.df2 <- data.frame(Colour = c('Blue','Blue','Blue','Blue','Blue','Blue','Red','Red','Red','Red','Red','Red'),
                     Type = c(1,1,2,2,2,'-',1,1,2,2,2,'-'),
                     Size = c('S','M','S','M','-','-','S','M','S','M','-','-'),
                     PriceOneMean = NA,
                     PriceOneStDev = NA,
                     PriceTwoMean = NA,
                     PriceTwoStDev = NA)

    my.df2
Colour Type Size PriceOneMean PriceOneStDev PriceTwoMean PriceTwoStDev
1    Blue    1    S           NA            NA           NA            NA
2    Blue    1    M           NA            NA           NA            NA
3    Blue    2    S           NA            NA           NA            NA
4    Blue    2    M           NA            NA           NA            NA
5    Blue    2    -           NA            NA           NA            NA
6    Blue    -    -           NA            NA           NA            NA
7     Red    1    S           NA            NA           NA            NA
8     Red    1    M           NA            NA           NA            NA
9     Red    2    S           NA            NA           NA            NA
10    Red    2    M           NA            NA           NA            NA
11    Red    2    -           NA            NA           NA            NA
12    Red    -    -           NA            NA           NA            NA

编辑:我已将第5行和第11行添加到my.df2,以便更好地匹配原始数据集。我如何才能使我的问题在这些行上面工作呢?

3 个答案:

答案 0 :(得分:3)

你可以尝试

library(tidyverse)
as.tbl(my.df1) %>% 
  mutate(Type=NA, Size=NA) %>% 
  bind_rows(my.df1) %>% 
  group_by(Colour, Type, Size) %>% 
  summarise_all(c("mean", "sd"))
# A tibble: 10 x 7
# Groups:   Colour, Type [?]
   Colour  Type   Size PriceOne_mean PriceTwo_mean PriceOne_sd PriceTwo_sd
   <fctr> <dbl> <fctr>         <dbl>         <dbl>       <dbl>       <dbl>
 1   Blue     1      M      12.66667      15.33333    3.055050    5.507571
 2   Blue     1      S      14.66667      15.00000    4.509250    5.000000
 3   Blue     2      M      17.33333      19.33333    5.507571    8.504901
 4   Blue     2      S      16.33333      14.00000    4.725816    4.000000
 5   Blue    NA   <NA>      15.25000      15.91667    4.287932    5.534328
 6    Red     1      M      15.33333      12.66667    5.507571    3.055050
 7    Red     1      S      15.00000      14.66667    5.000000    4.509250
 8    Red     2      M      19.33333      17.33333    8.504901    5.507571
 9    Red     2      S      14.00000      16.33333    4.000000    4.725816
10    Red    NA   <NA>      15.91667      15.25000    5.534328    4.287932

参考您的修改,我会这样做:

as.tbl(my.df1) %>% 
  bind_rows(mutate(my.df1, Type=NA, Size=NA)) %>% 
  bind_rows(mutate(my.df1, Size=NA)) %>% 
  group_by(Colour, Type, Size) %>% 
  summarise_all(c("mean", "sd"))

答案 1 :(得分:1)

dplyr库允许您分组,汇总和绑定。编辑添加额外的分组。为了简洁起见,我更喜欢@ Jimbou的回答 - 这可能是他/她的一行编辑。

my.df1 <- data.frame(Colour = c('Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Blue','Red','Red','Red','Red','Red','Red','Red','Red','Red','Red','Red','Red'),
                     Type = c(1,1,2,2,1,2,1,1,2,2,1,2,1,1,2,2,1,2,1,1,2,2,1,2),
                     Size = c('S','S','S','S','S','S','M','M','M','M','M','M','S','S','S','S','S','S','M','M','M','M','M','M'),
                     PriceOne = c(10,15,20,18,19,11,12,16,20,21,10,11,10,15,10,18,20,14,21,15,28,19,10,11),
                     PriceTwo = c(10,15,10,18,20,14,21,15,28,19,10,11,10,15,20,18,19,11,12,16,20,21,10,11))

library(dplyr)
# make detailed summaries
my.df1.ColourTypeSize = my.df1 %>%
  group_by(Colour, Type, Size) %>%
  summarise(
    PriceOneMean = mean(PriceOne),
    PriceOneStDev = sd(PriceOne),
    PriceTwoMean = mean(PriceTwo),
    PriceTwoStDev = sd(PriceTwo))

my.df1.ColourType = my.df1 %>%
  group_by(Colour, Type) %>%
  summarise(
    PriceOneMean = mean(PriceOne),
    PriceOneStDev = sd(PriceOne),
    PriceTwoMean = mean(PriceTwo),
    PriceTwoStDev = sd(PriceTwo)) %>%
  mutate(Size = NA)

# Make summary for colour alone and add NA for Size and Type
my.df1.Colour = my.df1 %>% 
  group_by(Colour) %>%
  summarise(
    PriceOneMean = mean(PriceOne),
    PriceOneStDev = sd(PriceOne),
    PriceTwoMean = mean(PriceTwo),
    PriceTwoStDev = sd(PriceTwo)) %>%
  mutate(Type = NA, Size = NA)

# Bind the summaries together and sort and arrange to make it look nice
my.df2 = 
  my.df1.Colour %>% 
  bind_rows(my.df1.ColourTypeSize) %>%
  bind_rows(my.df1.ColourType) %>%
  arrange(Colour, Type, Size) %>%
  select(Colour, Type, Size, everything())

答案 2 :(得分:0)

创建要在子集函数中调用的特征的所有可用组合:

call_combo <- function(frame) {
combo_list <- list()
for(i in 1:nrow(frame)) {
    combo <- frame[i,c(1,2,3)]
    combo_left <- combo[combo != '-']
    combo_left_cols <- names(combo[1:length(combo_left)])
    call_string <- paste(combo_left_cols, '==', combo_left, '&', sep=' ', collapse=' ')
    ind <- unlist(gregexpr('&',call_string))
    res <- substring(call_string, 1, ind[length(ind)]-1)
    combo_list[i] <- list(res)
}
    return(combo_list)
}

特征组合:

combo_list <- call_combo(my.df2)

combo_list

enter image description here

评估子集内的所有组合并附加到第二个数据框:

# define attributes as objects 
Blue <- 'Blue'
Red <- 'Red'
S <- 'S'
M <- 'M'
L <- 'L'

# evaluate combo_list entries inside subset function
for(p in 1:length(combo_list)) {
sub_frame <- subset(my.df1, eval(parse(text=combo_list[[p]])))

# calculate sd and mean for each combination and attach to 2nd frame 
my.df2[p,]$PriceOneStDev <- sd(sub_frame$PriceOne)
my.df2[p,]$PriceTwoStDev <- sd(sub_frame$PriceTwo)
my.df2[p,]$PriceOneMean <- mean(sub_frame$PriceOne)
my.df2[p,]$PriceTwoMean <- mean(sub_frame$PriceTwo)
}

<强>结果:

my.df2

enter image description here

相关问题