简体   繁体   English

在 R 中,如何从另一个表创建具有唯一行的表,然后将新列添加到新表

[英]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.

相关问题 在R中使用表格中的列创建新表格 - Using Columns from Table in R to create New Table 在表格的R中,如何通过为每个主题从另一行减去另一行来创建新行 - In R for a table, how to create new rows by substracting one row from another for each subject 如何使用多列从 R 中的现有表创建新表 - How to create a new table from an existing table in R, using multiple columns 如何从一个表,另一个表中查找搜索词,然后在结果中创建新列? - How to find search words from a table, in another table, and then create new columns of the results? 从 R 表中的 2 列创建一个新的百分比变化列 - Create a new percent change column from 2 columns in a table in R 通过R data.table中的ID删除重复的行,但添加一个新列,并将其连接的日期与另一列 - Remove duplicated rows by ID in R data.table, but add a new column with the concatenated dates from another column R dplyr pivot(排名表) - 具有来自多列的唯一行值的新列,然后从另一列分配行值 - R dplyr pivot (rankings table) - new column with unique row values from multiple columns, then assign row values from another column R data.table选择2继续行以创建新表 - R data.table pick 2 continue rows to create new table 如何创建一个表,其中第一个表中的行也匹配 R 中第二个表的行中的 3 列 - How do I create a table with rows from a first table that also matches 3 columns in the row of a second table in R 如何从R表的开始和结束时间列创建新的“时差”列 - How to create a new “time difference” column from start and finish time columns in an R table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM