简体   繁体   English

R如果is.na()为True则执行不起作用的函数; 条件的长度> 1,只使用第一个元素

[英]R if is.na() is True then perform a function not working; the condition has length > 1 and only the first element will be used

I'm trying to use an if statement that says if a value is.na, then perform a function on a different column. 我正在尝试使用if语句来说明值是否为.na,然后在不同的列上执行函数。

I can't get it to work, and I keep getting an error: 我无法让它工作,我一直收到错误:

  the condition has length > 1 and only the first element will be used

I've looked at the other questions regarding if statements, but I don't need to substitute one value for another. 我已经查看了有关if语句的其他问题,但我不需要将一个值替换为另一个值。 Instead, I need to run a function if is.na() = TRUE, and the function that I'm using (mutate_geocode) automatically makes the new columns, so I don't need to assign it to a new column. 相反,如果is.na()= TRUE,我需要运行一个函数,而我正在使用的函数(mutate_geocode)会自动生成新列,因此我不需要将它分配给新列。 Here's what I've been trying: 这是我一直在尝试的:

library(dplyr)
library(ggmap)

Enrollment_Report2 <- if (is.na(Enrollment_Report$lon)) {
  mutate_geocode(facility_city)
}

A sample of the data looks like this: 数据样本如下所示:

library(dplyr)
Enrollment_Report <- tibble(facility_city = c("Atlanta", "Boston", "Tokyo"),
lon = c(NA, NA, 139.65),
lat = c(NA, NA, 35.68))

We can filter out the NA rows and then apply the mutate_geocode 我们可以filter出NA行,然后应用mutate_geocode

library(dplyr)
library(ggmap)
Enrollment_Report %>% 
   filter(is.na(lon)) %>%
   summarise(fac_city = list(facility_city), 
            out = list(geocode(facility_city))) %>% 
   unnest %>% 
   rename(facility_city = fac_city) %>% 
   bind_rows(Enrollment_Report %>%
   filter(!is.na(lon)))
# A tibble: 3 x 3
# facility_city   lon   lat
#  <chr>         <dbl> <dbl>
#1 Atlanta       -84.4  33.7
#2 Boston        -71.1  42.4
#3 Tokyo         140.   35.7

Or create a logical index and then update the rows 或者创建逻辑索引然后更新行

i1 <- is.na(Enrollment_Report$lon)
Enrollment_Report[i1, -1] <- geocode(Enrollment_Report$facility_city[i1])
Enrollment_Report
# A tibble: 3 x 3
#  facility_city   lon   lat
#  <chr>         <dbl> <dbl>
#1 Atlanta       -84.4  33.7
#2 Boston        -71.1  42.4
#3 Tokyo         140.   35.7

I have to open a new answer as I do not have enough reputation to comment on akrun's reply. 我必须打开一个新答案,因为我没有足够的声誉评论akrun的回复。 The ifelse function does what you are looking for. ifelse函数可以满足您的需求。 The error that you get is because if looks for a singular value as stated above, but you pass in a vector. 您得到的错误是因为如果查找如上所述的奇异值,但您传入向量。 Here is a small example: 这是一个小例子:

a <- c(NA, 1, NA, 0)
if(is.na(a)){}
# NULL
# Warning message:
# In if (is.na(a)) { :
# the condition has length > 1 and only the first element will be used

If you instead wrote 如果你改写了

result <- rep(NA, 4)
for(i in 1:length(a)){
  if(is.na(a[i])){
    result[i] <- 1
  } else {
    result[i] <- 0
  }
}
result
# [1] 1 0 1 0

you don't get the error, because you are only using singular values with if(). 你没有得到错误,因为你只使用if()的奇异值。 Now, instead of using a for-loop and an if() else() clause, you can simply use the vectorized ifelse() function as suggested above. 现在,您可以简单地使用上面建议的矢量化ifelse()函数,而不是使用for循环和if()else()子句。 For every element of the vector Enrollment_Report$lon, it checkts if it is a NA value, and if so, it applies the function mutate_geocode on Enrollment_Report$facility_city, and if not, it just returns facility city. 对于向量Enrollment_Report $ lon的每个元素,它检查它是否是NA值,如果是,它在Enrollment_Report $ facility_city上应用函数mutate_geocode,如果不是,它只返回设施城市。

Enrollment_Report2 <- ifelse(is.na(Enrollment_Report$lon), mutate_geocode(Enrollment_Report$facility_city), Enrollment_Report$facility_city)

It is a vectorized version of if(){} else(){}. 它是if(){} else(){}的矢量化版本。

I dont have the Google API to test, but i think this could should work: 我没有Google API进行测试,但我认为这可能会有效:

library(ggmap)
library(dplyr)
library(hablar)

Enrollment_Report %>% 
  mutate(geocode = if_else_(is.na(lon), geocode(facility_city), NA))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 条件长度 &gt; 1 并且仅使用第一个元素 - 在 R - the condition has length > 1 and only the first element will be used - in R 错误:条件的长度&gt; 1,并且在r中仅使用第一个元素 - Error: condition has length > 1 and only the first element will be used in r R if 语句错误:条件长度 &gt; 1 且仅使用第一个元素 - R if statement error: the condition has length > 1 and only the first element will be used R If 语句返回“条件长度 &gt; 1 且仅使用第一个元素” - R If statement returns “the condition has length > 1 and only the first element will be used” if 函数中的错误:条件的长度 &gt; 1,并且只会使用第一个元素 - Error in the if function: the condition has length > 1 and only the first element will be used R警告:条件的长度&gt; 1,只使用第一个元素。 外在的功能 - R Warning: the condition has length > 1 and only the first element will be used. outer function 条件的长度&gt; 1,并且仅使用第一个元素 - The condition has length > 1 and only the first element will be used 条件的长度 &gt; 1 并且只有第一个元素 r - condition has length > 1 and only the first element r 为什么 is.na() 在 R 中的空向量的第一个元素上返回 TRUE? - Why does is.na() return TRUE on the first element of an empty vector in R? 如何在 R 中解决这个问题:在 while (t &lt;= cc[i]) { ... : 条件长度 &gt; 1 并且只使用第一个元素 - how to solve this problem in R: In while (t <= cc[i]) { ... : the condition has length > 1 and only the first element will be used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM