【问题标题】:Append values from one row to a new column in a separate row将一行中的值附加到单独行中的新列
【发布时间】:2021-09-02 21:12:26
【问题描述】:

在我的数据框中,我有两行通过变量“groupID”相关联。具体来说,相关行具有相同的 groupID 值(例如,两行的值都是 5)。

我想从相关行中获取特定值,并将感兴趣的值附加到相关行的新列中。另一种解释:如果第 1 行和第 4 行相关(因为它们共享一个 groupID),我想从第 1 行的 A 列中取出值并将其附加到第 4 行的新列中。同时,我想从第 4 行的 A 列中获取值并将其附加到第 1 行的新列。为了区分新列名和原始列名,我想为新列添加一个后缀(例如 columnA_teammate) .我没有尝试过,因为我不知道从哪里开始。

在下面提供的示例数据中,我想对 columnA 和 columnB 完成上述操作,而不是对 columnC。

df <- 
  data.frame(personID = c(1:16),
         groupID = c(1:8),
         columnA = rnorm(16, mean = 4, sd = 1),
         columnB = seq(2,32,by=2),
         columnC = rbinom(16, 1, .7)
         )

感谢您的帮助!

【问题讨论】:

  • 如果有两个以上的相关行会怎样?

标签: r data-manipulation data-cleaning data-wrangling


【解决方案1】:

你可以使用dplyr:

library(dplyr)

df %>% 
  left_join(df, by = "groupID", suffix = c("", "_teammate")) %>% 
  filter(personID != personID_teammate) %>% 
  select(-personID_teammate, -columnC_teammate)

返回

   personID groupID  columnA columnB columnC columnA_teammate columnB_teammate
1         1       1 4.162857       2       1         5.492979               18
2         2       2 4.377751       4       0         4.232240               20
3         3       3 4.721330       6       1         6.116825               22
4         4       4 5.372810       8       0         6.111737               24
5         5       5 5.260699      10       1         2.649022               26
6         6       6 4.055667      12       0         2.569150               28
7         7       7 1.913988      14       1         3.810043               30
8         8       8 1.643768      16       0         4.905459               32
9         9       1 5.492979      18       0         4.162857                2
10       10       2 4.232240      20       1         4.377751                4
11       11       3 6.116825      22       1         4.721330                6
12       12       4 6.111737      24       1         5.372810                8
13       13       5 2.649022      26       1         5.260699               10
14       14       6 2.569150      28       1         4.055667               12
15       15       7 3.810043      30       1         1.913988               14
16       16       8 4.905459      32       1         1.643768               16

数据

df <- structure(list(personID = 1:16, groupID = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), columnA = c(4.1628566514354, 
4.37775120677026, 4.72132999511972, 5.37280989644003, 5.2606991455534, 
4.05566700843516, 1.91398769391631, 1.6437683999418, 5.49297883127052, 
4.23224045206759, 6.11682482265615, 6.11173734702799, 2.64902184119363, 
2.5691503674088, 3.81004257186055, 4.90545868691805), columnB = c(2, 
4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32), columnC = c(1L, 
0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-16L))

【讨论】:

    【解决方案2】:

    这样做:

    library(tidyverse)
    df <- 
      data.frame(personID = c(1:16),
                 groupID = c(1:8),
                 columnA = rnorm(16, mean = 4, sd = 1),
                 columnB = seq(2,32,by=2),
                 columnC = rbinom(16, 1, .7)
      )
    
    fchange = function(data) tibble(column4 = c(data$columnA[2], data$columnA[1]))
    
    df1 = df %>% group_by(groupID) %>% 
      nest(data=!contains("groupID")) %>% 
      mutate(col4 = map(data, fchange)) %>% 
      unnest(c(data, col4)) %>% arrange(personID )
    

    输出

    # A tibble: 16 x 6
    # Groups:   groupID [8]
       groupID personID columnA columnB columnC column4
         <int>    <int>   <dbl>   <dbl>   <int>   <dbl>
     1       1        1    2.98       2       0    5.06
     2       2        2    2.83       4       0    4.92
     3       3        3    3.84       6       1    4.54
     4       4        4    4.52       8       1    6.29
     5       5        5    3.07      10       0    4.52
     6       6        6    2.64      12       0    3.20
     7       7        7    3.95      14       1    4.64
     8       8        8    5.24      16       1    3.17
     9       1        9    5.06      18       1    2.98
    10       2       10    4.92      20       1    2.83
    11       3       11    4.54      22       1    3.84
    12       4       12    6.29      24       1    4.52
    13       5       13    4.52      26       1    3.07
    14       6       14    3.20      28       1    2.64
    15       7       15    4.64      30       1    3.95
    16       8       16    3.17      32       1    5.24
    

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多