根据数据框中列的特定值提取特定行的值

时间:2019-09-09 03:06:27

标签: r dplyr

我是R的新手,正在研究语言数据。我想对dplyr进行处理,就是通过匹配另一列的值来提取特定行的值。

我尝试在mutute中使用which(),但是它不起作用。使用索引进行了尝试,但是它有自己的问题(如下所示)。

例如,假设我有:

library(dplyr)

df <- tibble(ID = c(1,1,1,2,2,3,3,3,4,4),
             year = c(2013,2014,2015,2013,2015,2013,2014,2015,2013,2015),
             Income = c(49, 32, 47, 14, 15, 14, 46, 45, 16, 42),
             Sales = c(12, 21, 42, 30, 10, 19, 16, 27, 18, 32))

最终,我想从上一年中扣除某个值。例如,(2014年收入)-(2013年收入)。我想做的是使用dplyr,方法与在基数R中调用df$Income[df$year=="2014"]的方式类似。

我不喜欢的原因:

dftemp <- df %>%
  group_by(ID) %>%
  mutate(Income14minus13 = Income[2] - Income[1])

是因为索引没有考虑数据中的2014年Missin,所以我想确保提取的是准确值。

我也尝试了此尝试,但没有成功:

dftemp <- df %>%
enter code here`group_by(ID) %>%
mutate(Income13 = Income[which(year==2013)],
         Income14 = Income[which(year==2014)],
         Income14minus13 = Income14 - Income13)

最后,我希望将其作为输出:

> desired_data
# A tibble: 10 x 7
      ID  year Income Sales Income13 Income14 Income15
   <dbl> <dbl>  <dbl> <dbl>    <dbl>    <dbl>    <dbl>
 1     1  2013     49    12       49       32       47
 2     1  2014     32    21       49       32       47
 3     1  2015     47    42       49       32       47
 4     2  2013     14    30       14       NA       15
 5     2  2015     15    10       14       NA       15
 6     3  2013     14    19       14       46       45
 7     3  2014     46    16       14       46       45
 8     3  2015     45    27       16       46       45
 9     4  2013     16    18       16       NA       42
10     4  2015     42    32       16       NA       42

我注意到case-when()仅在单行中生成变量,因此不允许进行逐行运算,而我想要的输出却可以这样做。

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

也许在这里加入会有所帮助?

df %>%
  left_join(by = "ID",
            df %>%
              select(ID, year, Income) %>%
              mutate(year = paste0("Income", year)) %>%
              tidyr::spread(year, Income)
  )

# A tibble: 10 x 7
      ID  year Income Sales Income2013 Income2014 Income2015
   <dbl> <dbl>  <dbl> <dbl>      <dbl>      <dbl>      <dbl>
 1     1  2013     49    12         49         32         47
 2     1  2014     32    21         49         32         47
 3     1  2015     47    42         49         32         47
 4     2  2013     14    30         14         NA         15
 5     2  2015     15    10         14         NA         15
 6     3  2013     14    19         14         46         45
 7     3  2014     46    16         14         46         45
 8     3  2015     45    27         14         46         45
 9     4  2013     16    18         16         NA         42
10     4  2015     42    32         16         NA         42

答案 1 :(得分:0)

也许另一种方法可能是将数据从长到宽重塑;缺失的值将自动变为NA(或者您可以使用fill指定一个值)。

例如

df %>%
    select(-Sales) %>%
    spread(year, Income) %>%
    mutate(Income14minus13 = `2014` - `2013`)
## A tibble: 4 x 5
#     ID `2013` `2014` `2015` Income14minus13
#  <dbl>  <dbl>  <dbl>  <dbl>           <dbl>
#1     1     49     32     47             -17
#2     2     14     NA     15              NA
#3     3     14     46     45              32
#4     4     16     NA     42              NA