Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环以查看dataframe1中的列值是否与dataframe2中的列值匹配_R_Loops_For Loop_Dataframe - Fatal编程技术网

循环以查看dataframe1中的列值是否与dataframe2中的列值匹配

循环以查看dataframe1中的列值是否与dataframe2中的列值匹配,r,loops,for-loop,dataframe,R,Loops,For Loop,Dataframe,以下是我想做的: 编写一个for循环,检查数据帧1中一列的值是否在数据帧2中的特定列中,然后将数据帧2中的两列添加到数据帧1。听起来很简单,对吧 这就是我到目前为止所做的: ID <- c(seq(1:5)) zip_codes <- c("47304", "46011", "47305", "46033", "46044") data <- data.frame(ID, zip_codes) library(zipcode) data("zipcode") dat

以下是我想做的:

编写一个for循环,检查数据帧1中一列的值是否在数据帧2中的特定列中,然后将数据帧2中的两列添加到数据帧1。听起来很简单,对吧

这就是我到目前为止所做的:

ID <- c(seq(1:5))

zip_codes <- c("47304", "46011", "47305", "46033", "46044")

data <- data.frame(ID, zip_codes)

library(zipcode)

data("zipcode")


data_zip <- zipcode[1:25000, c("zip", "latitude", "longitude")]

data$lat <- 0
data$long <- 0

for (i in data$zip_codes){
  if (i %in% data_zip[,1]) {
    data$lat <- data_zip[i, 2]
    data$long <- data_zip[i, 3]
  }
}
之后:

  ID zip_codes lat long
1  1     47304  NA   NA
2  2     46011  NA   NA
3  3     47305  NA   NA
4  4     46033  NA   NA
5  5     46044  NA   NA

我非常感谢任何指点——也许我想得太多了,还有一个更简单的解决方案……

这里有三个选项:

基带R无环 您还可以使用
匹配
避免循环:

cbind(data, data_zip[match(data$zip_codes, data_zip$zip), ])

      ID zip_codes   zip latitude longitude
21464  1     47304 47304 40.21540 -85.43636
20815  2     46011 46011 40.11291 -85.73700
21465  3     47305 47305 40.19229 -85.38494
20826  4     46033 46033 39.97373 -86.08875
20835  5     46044 46044 40.22121 -85.77612
甚至:
cbind(data,data\u-zip[match(data$zip\u-codes,data\u-zip$zip),-1])
以消除重复的
zip
列(一旦您“选中”了
match
执行任务)。此选项不需要额外的包,并且可能比循环选项快得多

基R-环 如果您确实想要一个循环(您的循环没有正确地将值分配给
数据$lat/long
),这里有两个:

# this one around your original code
for (i in 1:nrow(data)){
    data$lat[i]  <- data_zip[data_zip$zip == data$zip_codes[i], "latitude"]
    data$long[i] <- data_zip[data_zip$zip == data$zip_codes[i], "longitude"]
}

# shorter alternative
for (i in 1:nrow(data)){
  data[i, 3:4]  <- data_zip[data_zip$zip == data$zip_codes[i], c("latitude", "longitude")]
}
#这是围绕您的原始代码编写的
用于(i/1:nrow(数据)){

data$lat[i]对于循环
,您也可以使用合并函数而不是

library(dplyr)
df <- merge(data, data_zip, by.x = "zip_codes", by.y = "zip", all.x = T) %>%
      arrange(ID) %>% select(ID, zip_codes, lat = latitude, long = longitude)

> df
  ID zip_codes      lat      long
1  1     47304 40.21540 -85.43636
2  2     46011 40.11291 -85.73700
3  3     47305 40.19229 -85.38494
4  4     46033 39.97373 -86.08875
5  5     46044 40.22121 -85.77612
库(dplyr)
df%
排列(ID)%>%选择(ID,邮政编码,纬度=纬度,长=经度)
>df
ID邮政编码长
1  1     47304 40.21540 -85.43636
2  2     46011 40.11291 -85.73700
3  3     47305 40.19229 -85.38494
4  4     46033 39.97373 -86.08875
5  5     46044 40.22121 -85.77612

谢谢你,文森特!我一定会去看看-你认为我的for循环有没有办法工作-只是出于好奇?太好了!再次谢谢你!
library(dplyr)
df <- merge(data, data_zip, by.x = "zip_codes", by.y = "zip", all.x = T) %>%
      arrange(ID) %>% select(ID, zip_codes, lat = latitude, long = longitude)

> df
  ID zip_codes      lat      long
1  1     47304 40.21540 -85.43636
2  2     46011 40.11291 -85.73700
3  3     47305 40.19229 -85.38494
4  4     46033 39.97373 -86.08875
5  5     46044 40.22121 -85.77612