[英]R: Sequential Single Column to Two Columns
I have a dataframe like the following, where each individual ( ID
) travels to a location sequentially.我有一个如下所示的 dataframe,其中每个人 (
ID
) 按顺序前往一个位置。 I would like to restructure this dataframe so that I can get a From
column and a To
column for each ID我想重组这个 dataframe 以便我可以获得每个 ID 的
From
列和To
列
ID location order
1 6141500 1
1 6140690 2
2 6141450 1
2 6140430 2
2 6141450 3
Ideal output:理想 output:
From To
6141500 6140690
6141450 6140430
6140430 6141450
df %>%
group_by(ID)%>%
summarise(from = location, to = lead(location),.groups = 'drop')%>%
na.omit()
# A tibble: 3 × 3
ID from to
<int> <int> <int>
1 1 6141500 6140690
2 2 6141450 6140430
3 2 6140430 6141450
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.