[英]If/Else statement in R
我在R中有兩個數據幀:
city price bedroom
San Jose 2000 1
Barstow 1000 1
NA 1500 1
要重新創建的代碼:
data = data.frame(city = c('San Jose', 'Barstow'), price = c(2000,1000, 1500), bedroom = c(1,1,1))
和:
Name Density
San Jose 5358
Barstow 547
要重新創建的代碼:
population_density = data.frame(Name=c('San Jose', 'Barstow'), Density=c(5358, 547));
我想根據條件在data
集中創建一個名為city_type
的附加列,因此如果城市人口密度高於1000,則它是城市,低於1000是郊區,NA是NA。
city price bedroom city_type
San Jose 2000 1 Urban
Barstow 1000 1 Suburb
NA 1500 1 NA
我正在為條件流使用for循環:
for (row in 1:length(data)) {
if (is.na(data[row,'city'])) {
data[row, 'city_type'] = NA
} else if (population[population$Name == data[row,'city'],]$Density>=1000) {
data[row, 'city_type'] = 'Urban'
} else {
data[row, 'city_type'] = 'Suburb'
}
}
for循環在原始數據集中運行時沒有錯誤,觀察次數超過20000; 然而,它產生了許多錯誤的結果(它在大多數情況下產生NA)。
這里出了什么問題,我怎樣才能更好地達到我想要的結果呢?
對於這種類型的連接/過濾/變異工作流程,我已成為dplyr
管道的粉絲。 所以這是我的建議:
library(dplyr)
# I had to add that extra "NA" there, did you not? Hm...
data <- data.frame(city = c('San Jose', 'Barstow', NA), price = c(2000,1000, 500), bedroom = c(1,1,1))
population <- data.frame(Name=c('San Jose', 'Barstow'), Density=c(5358, 547));
data %>%
# join the two dataframes by matching up the city name columns
left_join(population, by = c("city" = "Name")) %>%
# add your new column based on the desired condition
mutate(
city_type = ifelse(Density >= 1000, "Urban", "Suburb")
)
輸出:
city price bedroom Density city_type
1 San Jose 2000 1 5358 Urban
2 Barstow 1000 1 547 Suburb
3 <NA> 500 1 NA <NA>
使用ifelse
在population_density
創建city_type
,然后我們使用match
population_density$city_type=ifelse(population_density$Density>1000,'Urban','Suburb')
data$city_type=population_density$city_type[match(data$city,population_density$Name)]
data
city price bedroom city_type
1 San Jose 2000 1 Urban
2 Barstow 1000 1 Suburb
3 <NA> 1500 1 <NA>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.