操纵数据框

时间:2020-06-25 11:53:22

标签: r dplyr

仍然是R语言的初学者,并希望获得有关处理数据帧Keyword Name | Volume | Date 1 | Date 2 | Date 3 | ... | Date N iphone 6 | 1600 | 25 | 28 | 29 | ... | Pos at Date N 的两个问题的帮助,

df

enter image description here

(1)变量structure(list(country = c("Brazil", "Brazil", "Brazil", "France", "France", "France"), date = structure(c(18353, 18354, 18355, 18353, 18354, 18355), class = "Date"), group = c(1, 1, 1, 1, 1, 1), share = c(0.00480432389150235, 0.00576122906219994, 0.00577170418006431, 0.0883595235712141, 0.0959849212276397, 0.104823151125402), max_share = c(0.110235379599597, 0.110235379599597, 0.110235379599597, 0.122052589288188, 0.122052589288188, 0.122052589288188)), row.names = c(NA, 6L), class = "data.frame") 是在初始max_share和结束share之间观察到的每个country的最大值date。我想出了一种相当笨拙的方法来将此变量添加到date中,并且想知道是否有更有效的方法来执行此操作。这是我所做的:

df

(2)我想在现有的df1 <- df %>% group_by(country) %>% summarize(max_share=max(share)) %>% select(country, max_share) df <- merge(df, df1, all=TRUE, by.x="country",by.y="country") 中创建如下两个子组。

我想添加另一个名为df的组变量。我想选择那些selection的国家,并为它们分配max_share>0.10的值1

接下来,我想向selection添加另一个名为df的{​​{1}}。 country观测值将在Rest of the world下标记为Rest of the world。请注意,由于我们有多个2的日期,因此我们将有几个新的观察结果。

然后我挣扎着。我希望将这些新观察的selection计算为Rest of the world下其他国家的份额之和。

我尝试了shareselection == 1,但是无法确定如何使行保持相同的尺寸,更改非数字变量的值以及仅对数字变量求和。可能有一种简单的方法可以做到这一点。只是想不通。

最后,df将包含原始国家/地区集,用于新观察值summarise的一组新行(每个日期有1行)以及一个名为{{1 }}。

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

问题1的答案:

df <- df %>%
  group_by(country) %>%
  mutate(max_share = max(share)) %>%
  ungroup()

答案 1 :(得分:0)

使用汇总进行“选择”

df$selection <- df$country %in% with(aggregate(share ~ country, data = df, FUN = max), country[share > 0.1]))

  country       date group       share selection
1  Brazil 2020-04-01     1 0.004804324         0
2  Brazil 2020-04-02     1 0.005761229         0
3  Brazil 2020-04-03     1 0.005771704         0
4  France 2020-04-01     1 0.088359524         1
5  France 2020-04-02     1 0.095984921         1
6  France 2020-04-03     1 0.104823151         1

答案 2 :(得分:0)

使用dplyr::case_whendplyr::replace_na

df2<-structure(list(country = c("Rest of the world", "Rest of the world", "Rest of the world"), date = structure(c(18353, 18354, 18355), class = "Date"), group = c(1, 1, 1), share = c(NA,NA,NA)), row.names = c(NA, 3L), class = "data.frame")
df %>%
  bind_rows(df2) %>% 
  group_by(country) %>% 
  mutate(max_share=max(share),
         selection=case_when(max_share > 0.10 & country != "Rest of the world" ~ 1,
                             country=="Rest of the world" ~ 2,
                             TRUE~0)) %>% 
  ungroup() %>% 
  mutate(share=replace_na(max_share,sum(share[selection==1])))

产生的df:

# A tibble: 9 x 6
  country           date       group   share max_share selection
  <chr>             <date>     <dbl>   <dbl>     <dbl>     <dbl>
1 Brazil            2020-04-01     1 0.00577   0.00577         0
2 Brazil            2020-04-02     1 0.00577   0.00577         0
3 Brazil            2020-04-03     1 0.00577   0.00577         0
4 France            2020-04-01     1 0.105     0.105           1
5 France            2020-04-02     1 0.105     0.105           1
6 France            2020-04-03     1 0.105     0.105           1
7 Rest of the world 2020-04-01     1 0.289    NA               2
8 Rest of the world 2020-04-02     1 0.289    NA               2
9 Rest of the world 2020-04-03     1 0.289    NA               2

df2为:

            country       date group share
1 Rest of the world 2020-04-01     1    NA
2 Rest of the world 2020-04-02     1    NA
3 Rest of the world 2020-04-03     1    NA

这就是你想要的吗?