[英]Combining pivoted rows in R by common value
I have a data frame that looks like this 我有一个看起来像这样的数据框
Name Visit Arrival Departure
Jack week 1 8:00 NA
Jack week 1 NA 8:30
Sally week 5 9:00 NA
Sally week 5 NA 9:30
Adam week 2 2:00 NA
Adam week 2 NA 3:00
The Arrival and Departure time were initially rows and i pivoted into colums which is why there are the nulls. 到达和离开时间最初是几行,我转到各列,这就是为什么有空值的原因。 I want to merge the rows base on name and visit so the arrival and departure are in the same row like
我想基于名称合并行并访问,因此到达和离开都在同一行中
Name Visit Arrival Departure
Jack week 1 8:00 8:30
Sally week 5 9:00 9:30
Adam week 2 2:00 3:00
Any solution would be appreciated, having a tough time trying to merge there. 任何解决方案将不胜感激,因为很难在此合并。
Just aggregate
it with na.omit
as the aggregation function: 只是
aggregate
与它na.omit
作为聚合函数:
aggregate(dat[c("Arrival","Departure")], dat[c("Name","Visit")], FUN=na.omit)
# or
aggregate(cbind(Arrival,Departure) ~ ., data=dat, FUN=na.omit, na.action=na.pass)
# Name Visit Arrival Departure
#1 Jack week1 8:00 8:30
#2 Adam week2 2:00 3:00
#3 Sally week5 9:00 9:30
Same logic works in data.table
: 相同的逻辑在
data.table
中data.table
:
dat[, lapply(.SD,na.omit), by=.(Name,Visit)]
...or dplyr
: ...或
dplyr
:
dat %>% group_by(Name,Visit) %>% summarise_all(na.omit)
Here's one approach, assuming that person who visited will have exactly two rows of data: 这是一种方法,假设访问的人将只拥有两行数据:
library(dplyr)
df = readr::read_table("Name Visit Arrival Departure
Jack week 1 8:00 NA
Jack week 1 NA 8:30
Sally week 5 9:00 NA
Sally week 5 NA 9:30
Adam week 2 2:00 NA
Adam week 2 NA 3:00", col_types="cccc")
df %>%
group_by(Name, Visit) %>%
mutate(Arrival = ifelse(is.na(Arrival), lag(Arrival), Arrival),
Departure = ifelse(is.na(Departure), lead(Departure), Departure)) %>%
ungroup() %>%
distinct(Name, Visit, .keep_all=TRUE)
# A tibble: 3 × 4
Name Visit Arrival Departure
<chr> <chr> <chr> <chr>
1 Jack week 1 8:00 8:30
2 Sally week 5 9:00 9:30
3 Adam week 2 2:00 3:00
I'm sure there might be a prettier way of doing this, but this is what worked for me: 我敢肯定,这样做可能更漂亮,但这对我有用:
library(data.table)
library(reshape2)
test <- data.table(Name = c("Jack", "Jack", "Sally", "Sally", "Adam", "Adam"), Visit = c("week 1", "week 1", "week 5", "week 5", "week 2", "week 2"), Arrival = c("8:00", NA, "9:00", NA, "2:00", NA), Departure = c(NA, "8:30", NA, "9:30", NA, "3:00"))
test_m <- melt(test,id.vars = c("Name", "Visit"))
test_m <- test_m[!is.na(value),]
test_c <- dcast(test_m, Name + Visit ~ variable)
> test_c
Name Visit Arrival Departure
1 Adam week 2 2:00 3:00
2 Jack week 1 8:00 8:30
3 Sally week 5 9:00 9:30
Hope that helps 希望能有所帮助
Actually, if you are able to get back to the data before the pivot, tidyr::spread will do a beautiful job. 实际上,如果您能够在数据透视之前返回数据,那么tidyr :: spread会做得很出色。
Name <- c("Jack", "Jack","Sally", "Sally", "Adam", "Adam")
Visit <- c("week1", "week1", "week5", "week5", "week2", "week2")
Itenary <- rep(c("Arrival", "Departure"), 3)
Time <- c("8:00", "8:30", "9:00", "9:30", "2:00", "2:30")
df <- data.frame(Name, Visit, Itenary, Time)
df
Name Visit Itenary Time
1 Jack week1 Arrival 8:00
2 Jack week1 Departure 8:30
3 Sally week5 Arrival 9:00
4 Sally week5 Departure 9:30
5 Adam week2 Arrival 2:00
6 Adam week2 Departure 2:30
df %>%
spread(key = Itenary, value = Time)
Name Visit Arrival Departure
1 Adam week2 2:00 2:30
2 Jack week1 8:00 8:30
3 Sally week5 9:00 9:30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.