[英]In R, How to create a table with unique rows from another table and then add new columns to new table
I have a table:我有一张桌子:
ID Latitude Longitude Visit_Date
1850 46.72187 -114.7254 7/1/2017
1850 46.72187 -114.7254 12/7/2018
1850 46.72187 -114.7254 6/13/2018
1850 46.72186 -114.7250 6/13/2018
1850 46.72186 -114.7250 6/8/2019
1850 46.72186 -114.7250 10/26/2019
I want to create a table that has a row for each unique lat long and the time frame that the ID was at that lat long.我想创建一个表,该表为每个唯一的经纬度和 ID 在该经纬度的时间范围内都有一行。 So ideally an output like this:
所以理想情况下是这样的输出:
ID Latitude Longitude Start End
1850 46.72187 -114.7254 7/1/2017 6/13/2018
1850 46.72186 -114.7250 6/13/2018 10/26/2019
I am a little lost on how to accomplish this.我对如何实现这一点感到有些茫然。 I was thinking using the shift() function to subtract rows from another and if the lat long != 0 then that would be a unique lat long.
我正在考虑使用 shift() 函数从另一个函数中减去行,如果 lat long != 0 那么这将是一个唯一的 lat long。 I was also thinking the unique() function would be useful but I'm not sure which direction would be best.
我还认为 unique() 函数会很有用,但我不确定哪个方向最好。 Any help will be useful.
任何帮助都会有用。 Thank you!
谢谢!
Using dplyr, you can do summary statistics pretty easily:使用 dplyr,您可以非常轻松地进行汇总统计:
df1 %>%
mutate(Visit_Date = as.Date(Visit_Date, format = "%m/%d/%Y")) %>%
group_by(ID, Latitude, Longitude) %>%
summarise(Start = min(Visit_Date),
End = max(Visit_Date))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.