i have this data frame:
ers1 task
t1 3 t1
t2 3 t2
t3 3 t3
t4 4 t4
t5 3 t5
t6 4 t6
t7 4 t7
t8 3 t8
The data describes the task distribution (8 different tasks) for a group of employees. i would like to aggregate the tasks for a given employee, in order to get something like this:
ers task1 task2 task3 task4 task5
3 t1 t2 t3 t5 t8
4 t4 t6 t7
Any suggestion? Thanks
Please see the following solution in base R for one creative approach.
d1 <- data.frame(ers1 = c(3,3,3,4,3,4,4,3), task = paste0("t",1:8)) # raw data
d2 <- table(d1) # use table to do the reshaping work here
l1 <- apply(d2, 1, function(x) colnames(d2)[index(x)*x]) # use the 0s and 1s to fill tasks
d3 <- t(sapply(l1, '[', seq(max(sapply(l1, length))))) # combine lists of varying lengths
colnames(d3) <- paste0("t",1:ncol(d3)) # create colnames
d3[is.na(d3)] <- "" # change NAs to blanks as desired
d3
# t1 t2 t3 t4 t5
#3 "t1" "t2" "t3" "t5" "t8"
#4 "t4" "t6" "t7" "" ""
Personally, I would stop after getting l1
(list 1) because it seems more useful for programming/applying functions.
thanks for letting me know the spread command from tidyverse library.
I solved the problem in one line
spread(ers1, key=task, value = task)
Also an alternative using split
from data.table
library(data.table)
split(setDT(d1),by=c("ers1"),keep.by = FALSE)
$`3`
task
1: t1
2: t2
3: t3
4: t5
5: t8
$`4`
task
1: t4
2: t6
3: t7
Then, to obtain your required format:
sapply( split(setDT(d1),by="ers1",keep.by = FALSE),'[',1:5)
$`3.task`
[1] t1 t2 t3 t5 t8
Levels: t1 t2 t3 t4 t5 t6 t7 t8
$`4.task`
[1] t4 t6 t7 <NA> <NA>
Levels: t1 t2 t3 t4 t5 t6 t7 t8
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.