簡體   English   中英

將數據框加入到 R 中的數據框列表中

[英]join a data frame to a list of data frames in R

我有一個參與者數據列表,其中包含心率 (HR) 和位置,我需要將這些數據與特定代碼中最近位置的天氣數據結合起來。 這是一個例子:

library(sf)
library(purrr)

#Participant1
HR<- c(60,62,61,60,60,61)
Code<- c("0_0", "0_1", "1_1", "0_2", "2_2", "0_0")
Lat <- c("1.295824", "1.295824", "1.295826", "1.295828", "1.295830", "1.295830")
Lon <- c("103.8494", "103.8494", "103.8494", "103.8494", "103.8494", "103.8494")
P1 <- data.frame(HR, Code, Lat, Lon)

#Participant2
HR<- c(71,70,69,71,72, 70)
Code<- c("0_0", "0_1", "1_1", "1_1", "0_2", "2_2")
Lat <- c("1.295995", "1.295977", "1.295995", "1.295992", "1.295987", "1.295992")
Lon <- c("103.8492", "103.8492", "103.8492", "103.8492", "103.8492", "103.8492")
P2 <- data.frame(HR, Code, Lat, Lon)

#Participant3
HR<- c(68,67,65,66,68, 68)
Code<- c("0_0", "0_1", "1_1", "0_2", "2_2", "2_2")
P3 <- data.frame(HR, Code, Lat, Lon)
Lat <- c("1.295773", "1.295770", "1.295769", "1.295769", "1.295772",  "1.295769")
Lon <- c("103.8493", "103.8493", "103.8493", "103.8493", "103.8493", "103.8493")

#Creating a list of df from participant data
ListP <- list(P1, P2, P3)
ListP <- lapply(ListP, function(x)  sf::st_as_sf(x, coords=c("Lat","Lon"))) # creating geometry from latitude and longitude

#Creating Weather df
Tair <- c(30, 31, 32, 30, 21)
Code<- c("0_0", "0_1", "1_1", "0_2", "2_2")
Lat <- c("1.296033", "1.296028", "1.296020","1.296013", "1.296008")
Lon <- c("103.8493", "103.8493", "103.8493", "103.8493", "103.8493")
Weather <- data.frame(Tair, Code, Lat, Lon)
Weather <- sf::st_as_sf(Weather, coords = c("Lat","Lon"))# creating geometry from latitude and longitude

現在我需要通過特定代碼中最近的位置點將天氣文件加入 ListP 中的每個參與者。 例如,代碼為“0_0”的天氣點只能與具有相同代碼“0_0”的參與者的點連接

我嘗試通過代碼拆分 ListP 和 Weather,所以我最終會得到 ListP 的嵌套列表和 Weather 的列表,如下所示:

#splitting into nested data frames by Code
ListP <- lapply(ListP, function(x) split(x, f=x$Code)) 
Weather <- split(x=Weather, f=Weather$Code)

現在我正在嘗試使用 st_join 加入兩者,但是,參與者 df 會覆蓋自身,所以我最終只會加入最后一個參與者數據。 我覺得我需要添加一些 function 將結果保存到單個參與者的新數據幀列表中,但不知道如何到達那里。

for(participant in ListP) 
  {
  merge <-purrr::map2(participant, Weather,
                          st_join,
                          join = st_nearest_feature) 
  }

非常感謝您的幫助!

library(sf)
library(purrr)

#Participant1
HR<- c(60,62,61,60,60,61)
Code<- c("0_0", "0_1", "1_1", "0_2", "2_2", "0_0")
Lat <- c("1.295824", "1.295824", "1.295826", "1.295828", "1.295830", "1.295830")
Lon <- c("103.8494", "103.8494", "103.8494", "103.8494", "103.8494", "103.8494")
P1 <- data.frame(HR, Code, Lat, Lon)

#Participant2
HR<- c(71,70,69,71,72, 70)
Code<- c("0_0", "0_1", "1_1", "1_1", "0_2", "2_2")
Lat <- c("1.295995", "1.295977", "1.295995", "1.295992", "1.295987", "1.295992")
Lon <- c("103.8492", "103.8492", "103.8492", "103.8492", "103.8492", "103.8492")
P2 <- data.frame(HR, Code, Lat, Lon)

#Participant3
HR<- c(68,67,65,66,68, 68)
Code<- c("0_0", "0_1", "1_1", "0_2", "2_2", "2_2")
P3 <- data.frame(HR, Code, Lat, Lon)
Lat <- c("1.295773", "1.295770", "1.295769", "1.295769", "1.295772",  "1.295769")
Lon <- c("103.8493", "103.8493", "103.8493", "103.8493", "103.8493", "103.8493")

#Creating a list of df from participant data
ListP <- list(P1, P2, P3)
ListP <- lapply(ListP, function(x)  sf::st_as_sf(x, coords=c("Lat","Lon"))) # creating geometry from latitude and longitude

#Creating Weather df
Tair <- c(30, 31, 32, 30, 21)
Code<- c("0_0", "0_1", "1_1", "0_2", "2_2")
Lat <- c("1.296033", "1.296028", "1.296020","1.296013", "1.296008")
Lon <- c("103.8493", "103.8493", "103.8493", "103.8493", "103.8493")
Weather <- data.frame(Tair, Code, Lat, Lon)
Weather <- sf::st_as_sf(Weather, coords = c("Lat","Lon"))# creating geometry from latitude and longitude

for(i in 1:length(ListP)) {

ListP[[i]] <- merge(ListP[[i]], Weather, by="Code", x.all = T)

}

我想我使用您提供的提示解決了它:

 for(i in 1:length(ListP)) 
   
  {ListP[[i]] <-purrr::map2(ListP[[i]], Weather,
                          st_join,
                          join = st_nearest_feature)}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM