简体   繁体   中英

R data reshape using dplyr

I have this data:

library(dplyr)

df1 <- tibble(
  type = c("Animals", "Animals", "People", "People"),
  type_group = c("Dogs", "Cats", "John", "Jane"),
  analysis1 = c(32.7, 67.5, 34.6, 56.5),
  analysis2 = c(23.7, 89.4, 45.8, 98.6),
  analysis3 = c(45.7, 45.7, 23.6, 23.6),
  analysis4 = c(14.4, 45.4, 98.0, 12.2))

I would like to add some new rows to the data so that it looks like this:

df2 <- tibble(
  type = c("Animals", "Animals", "Animals diff", "People", "People", "People diff"),
  type_group = c("Dogs", "Cats", "Dogs and cats" ,"John", "Jane", "John and Jane"),
  analysis1 = c(32.7, 67.5, 34.8, 34.6, 56.5, 21.9),
  analysis2 = c(23.7, 89.4, 65.7, 45.8, 98.6, 52.8),
  analysis3 = c(45.7, 45.7, 0.0,  23.6, 23.6, 0.0),
  analysis4 = c(14.4, 45.4, 31.0, 98.0, 12.2, 85.8))

The new rows are titled 'Animals diff', which is the cats figures minus the dogs figure. Similarly there is a new row call 'People diff' which is the Jane figures minus the john figures.

I know the simple way to do this would be to use dplyr and add the new rows as variables and make the data wider rather than longer. However that format won't work for what I want to do with the data. It specifically needs to be in this longer format as displayed in df2.

What I thought I could to is create the variables using mutate in dplyr to make the data wider, and then use reshape to make the data long but after playing around I can't think how to do that. Any ideas on how I can get to df2?

Thanks

A rbind couldn't do the trick?

> rbind(df1,c("People",'Me',1,2,3,4))
# A tibble: 5 x 6
  type    type_group analysis1 analysis2 analysis3 analysis4
  <chr>   <chr>      <chr>     <chr>     <chr>     <chr>    
1 Animals Dogs       32.7      23.7      45.7      14.4     
2 Animals Cats       67.5      89.4      45.7      45.4     
3 People  John       34.6      45.8      23.6      98       
4 People  Jane       56.5      98.6      23.6      12.2     
5 People  Me         1         2         3         4        

Do that for every new row or create the tibble with the new rows you need to add and bind both tibbles in the same way.

> rbind(df1,df2)
# A tibble: 10 x 6
   type         type_group    analysis1 analysis2 analysis3 analysis4
   <chr>        <chr>             <dbl>     <dbl>     <dbl>     <dbl>
 1 Animals      Dogs               32.7      23.7      45.7      14.4
 2 Animals      Cats               67.5      89.4      45.7      45.4
 3 People       John               34.6      45.8      23.6      98  
 4 People       Jane               56.5      98.6      23.6      12.2
 5 Animals      Dogs               32.7      23.7      45.7      14.4
 6 Animals      Cats               67.5      89.4      45.7      45.4
 7 Animals diff Dogs and cats      34.8      65.7       0        31  
 8 People       John               34.6      45.8      23.6      98  
 9 People       Jane               56.5      98.6      23.6      12.2
10 People diff  John and Jane      21.9      52.8       0        85.8

Think I managed to answer this using the rbind suggestion.

rbind(df1,c("People diff","John and Jane", 
        df1$analysis1[df1$type_group == 'John'] - df1$analysis1[df1$type_group == 'Jane'],
        df1$analysis2[df1$type_group == 'John'] - df1$analysis2[df1$type_group == 'Jane'],
        df1$analysis3[df1$type_group == 'John'] - df1$analysis3[df1$type_group == 'Jane'],
        df1$analysis4[df1$type_group == 'John'] - df1$analysis4[df1$type_group == 'Jane'])) -> jj

rbind(df1,c("Animals diff","Dogs and cats", 
        df1$analysis1[df1$type_group == 'Cats'] - df1$analysis1[df1$type_group == 
'Dogs'],
        df1$analysis2[df1$type_group == 'Cats'] - df1$analysis2[df1$type_group == 
'Dogs'],
        df1$analysis3[df1$type_group == 'Cats'] - df1$analysis3[df1$type_group == 
'Dogs'],
        df1$analysis4[df1$type_group == 'Cats'] - df1$analysis4[df1$type_group == 
'Dogs'])) -> c_d

rbind(jj, c_d)


# A tibble: 10 x 6
  type         type_group    analysis1 analysis2 analysis3 analysis4
<chr>        <chr>         <chr>     <chr>     <chr>     <chr>    
1 Animals      Dogs          32.7      23.7      45.7      14.4     
2 Animals      Cats          67.5      89.4      45.7      45.4     
3 People       John          34.6      45.8      23.6      98       
4 People       Jane          56.5      98.6      23.6      12.2     
5 People diff  John and Jane -21.9     -52.8     0         85.8     
6 Animals      Dogs          32.7      23.7      45.7      14.4     
7 Animals      Cats          67.5      89.4      45.7      45.4     
8 People       John          34.6      45.8      23.6      98       
9 People       Jane          56.5      98.6      23.6      12.2     
10 Animals diff Dogs and cats 34.8      65.7      0         31

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM