Grouped pivot_longer dplyr

孤人 提交于 2021-01-28 04:13:12

问题


This is an example dataframe. My real dataframe is larger. I highly prefer a tidyverse solution.

#my data
age <- c(18,18,19)
A1 <- c(3,5,3)
A2 <- c(4,4,3)
B1 <- c(1,5,2)
B2 <- c(2,2,5)
df <- data.frame(age, A1, A2, B1, B2)

I want my data to look like this:

#what i want
new_age <- c(18,18,18,18,19,19)
A <- c(3,5,4,4,3,3)
B <- c(1,5,2,2,2,5)
new_df <- data.frame(new_age, A, B)

I want to pivot longer and stack columns A1:A2 into column A, and B1:B2 into B. I also want to have the responses to match the correct age. For example, the 19 year old person in this example has only responded with 3's in columns A1:A2.


回答1:


tidyr::pivot_longer(df, cols = -age, names_to = c(".value",'groupid'), 
                    #1+ non digits followed by 1+ digits
                    names_pattern = "(\\D+)(\\d+)")
# A tibble: 6 x 4
    age groupid   A     B
  <dbl> <chr> <dbl> <dbl>
1    18 1         3     1
2    18 2         4     2
3    18 1         5     5
4    18 2         4     2
5    19 1         3     2
6    19 2         3     5



回答2:


in Base R you will use reshape then select the columns you want. You can change the row names also

reshape(df,2:ncol(df),dir = "long",sep="")[,-c(2,5)] # 
    age A B
1.1  18 3 1
2.1  18 5 5
3.1  19 3 2
1.2  18 4 2
2.2  18 4 2
3.2  19 3 5



回答3:


As you have a larger dataframe, maybe a solution with data.table will be faster. Here, you can use melt function from data.table package as follow:

library(data.table)
colA = grep("A",colnames(df),value = TRUE)
colB = grep("B",colnames(df),value = TRUE)
setDT(df)

df <- melt(df, measure = list(colA,colB), value.name = c("A","B"))
df[,variable := NULL]
dt <- dt[order(age)]

   age A B
1:  18 3 1
2:  18 5 5
3:  18 4 2
4:  18 4 2
5:  19 3 2
6:  19 3 5

Does it answer your question ?

EDIT: Using patterns - suggestion from @Wimpel

As @Wimpel suggested it in comments, you can get the same result using patterns:

melt( setDT(df), measure.vars = patterns( A="^A[0-9]", B="^B[0-9]") )[, variable:=NULL][]

   age A B
1:  18 3 1
2:  18 5 5
3:  19 3 2
4:  18 4 2
5:  18 4 2
6:  19 3 5


来源:https://stackoverflow.com/questions/60482724/grouped-pivot-longer-dplyr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!