简体   繁体   中英

Reshape and rearrange data frame in r

Year = c(2013, 2013, 2014, 2014, 2015, 2014, 2013, 2015, 2015);districts = c("A", "B", "C", "B","A", "A", "C" ,"C","B"); sex = c("M"," F", "M", "F","M"," F","M", "F", "F"); age = c("age1",'age2', 'age3','age4', "age5","age6", "age7","age8","age8");df = data.frame(Year, districts, sex, age); df

我想采用以下格式:

Year = c(2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015); districts = c("A", "B", "C", "A","B", "C", "A" ,"B","C"); sex = c("M"," F", "M", "F","F"," M","M", "F", "F"); age = c("age1",'age2', 'age3','age4', "age5","age6", "age7","age8","age8");df = data.frame(Year, districts, sex, age) ;df

We could arrange the columns and then check whether the next value in 'age' is the same as the current

library(dplyr)
res <- df %>% 
         arrange(Year, districts) %>% 
         mutate(age = paste0('age', cumsum(c(TRUE, age[-1] != age[-n()]))))


all.equal(res, out)
#[1] TRUE

NOTE: After removing the white spaces in 'sex' column in the original dataset ('df') and the expected output ('out'). Also, converted to character class for easy comparison

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