R ifelse() 评估条件并 returns 匹配
R ifelse() evaluates a condition and returns match
我有一个数据框
countryname <- c("Viet Nam", "Viet Nam", "Viet Nam", "Viet Nam", "Viet Nam")
year <- c(1974, 1975, 1976, 1977,1978)
df <- data.frame(countryname, year)
这是按年份格式的长国家/地区。
我想创建一个函数,可以根据观察年份对国家/地区名称进行标准化。我创建了一个能够从数据框 cnames
中提取并标准化名称的函数,但这仅对横截面有用,并且如果国家/地区名称不随时间变化。
country <- c("Vietnam, North", "Vietnam, N.", "Vietnam North", "Viet Nam", "Democratic Republic Of Vietnam")
standardize <- c("Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of")
panel <- c("Vietnam", "Vietnam","Vietnam","Vietnam","Vietnam")
time <- c(1976,1976,1976,1976,1976)
cnames <- data.frame(country, standardize, panel, time)
要标准化的函数是
country_name <- function(x) {
return(cnames[match(x,cnames$country),]$standardize)
}
但是,如您所见,这并未考虑国家/地区名称随时间的任何变化。我尝试了很多不同的东西,我最接近的是这个功能。
country_panel <- function(x, y) {
ifelse(cnames$time < y,
return(cnames[match(x, cnames$country),]$panel),
return(cnames[match(x, cnames$country),]$standardize)
)
}
我使用 dplyr
链拉入数据框,然后使用 mutate
创建一个新变量,理想情况下可以捕获国家/地区名称的差异。
d1 <- df %>%
mutate(new_name = country_panel(countryname, year))
我发现的问题是该函数仅将 country_panel
函数中的 y
作为单个对象计算,而不是作为向量计算。如果我输入一个大于或小于 cnames$time
的整数,它会正确评估但会传递每一行的值。
我怎样才能让这个函数评估每个 cnames$country
和 cnames$time
与 df$year
和 return 的正确 cnames$panel
或 cnames$standardize
的关系?
感谢您的帮助。
您可以根据年份和国家名称加入表格:
left_join(df, cnames, by = c("countryname" = "country", "year" = "time"))
countryname year standardize panel
1 Viet Nam 1974 <NA> <NA>
2 Viet Nam 1975 <NA> <NA>
3 Viet Nam 1976 Vietnam, Democratic Republic of Vietnam
4 Viet Nam 1977 <NA> <NA>
5 Viet Nam 1978 <NA> <NA>
d1
# countryname year new_name
# 1 Viet Nam 1974 Vietnam, Democratic Republic of
# 2 Viet Nam 1975 Vietnam, Democratic Republic of
# 3 Viet Nam 1976 Vietnam, Democratic Republic of
# 4 Viet Nam 1977 Vietnam
# 5 Viet Nam 1978 Vietnam
您需要做的就是确保在定义数据框时将它们设置为 stringsAsFactors=F
,即 (df <- data.frame(countryname, year, stringsAsFactors=F)
)。并取出return
命令:
country_panel <- function(x, y) {
ifelse(cnames$time < y,
cnames[match(x, cnames$country),]$panel,
cnames[match(x, cnames$country),]$standardize
)
}
其背后的原因是 return
一旦函数被调用就会停止其运行。因此,您的数据框由单个值输出填充。这就是为什么它们都是一样的。
我有一个数据框
countryname <- c("Viet Nam", "Viet Nam", "Viet Nam", "Viet Nam", "Viet Nam")
year <- c(1974, 1975, 1976, 1977,1978)
df <- data.frame(countryname, year)
这是按年份格式的长国家/地区。
我想创建一个函数,可以根据观察年份对国家/地区名称进行标准化。我创建了一个能够从数据框 cnames
中提取并标准化名称的函数,但这仅对横截面有用,并且如果国家/地区名称不随时间变化。
country <- c("Vietnam, North", "Vietnam, N.", "Vietnam North", "Viet Nam", "Democratic Republic Of Vietnam")
standardize <- c("Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of", "Vietnam, Democratic Republic of")
panel <- c("Vietnam", "Vietnam","Vietnam","Vietnam","Vietnam")
time <- c(1976,1976,1976,1976,1976)
cnames <- data.frame(country, standardize, panel, time)
要标准化的函数是
country_name <- function(x) {
return(cnames[match(x,cnames$country),]$standardize)
}
但是,如您所见,这并未考虑国家/地区名称随时间的任何变化。我尝试了很多不同的东西,我最接近的是这个功能。
country_panel <- function(x, y) {
ifelse(cnames$time < y,
return(cnames[match(x, cnames$country),]$panel),
return(cnames[match(x, cnames$country),]$standardize)
)
}
我使用 dplyr
链拉入数据框,然后使用 mutate
创建一个新变量,理想情况下可以捕获国家/地区名称的差异。
d1 <- df %>%
mutate(new_name = country_panel(countryname, year))
我发现的问题是该函数仅将 country_panel
函数中的 y
作为单个对象计算,而不是作为向量计算。如果我输入一个大于或小于 cnames$time
的整数,它会正确评估但会传递每一行的值。
我怎样才能让这个函数评估每个 cnames$country
和 cnames$time
与 df$year
和 return 的正确 cnames$panel
或 cnames$standardize
的关系?
感谢您的帮助。
您可以根据年份和国家名称加入表格:
left_join(df, cnames, by = c("countryname" = "country", "year" = "time"))
countryname year standardize panel
1 Viet Nam 1974 <NA> <NA>
2 Viet Nam 1975 <NA> <NA>
3 Viet Nam 1976 Vietnam, Democratic Republic of Vietnam
4 Viet Nam 1977 <NA> <NA>
5 Viet Nam 1978 <NA> <NA>
d1
# countryname year new_name
# 1 Viet Nam 1974 Vietnam, Democratic Republic of
# 2 Viet Nam 1975 Vietnam, Democratic Republic of
# 3 Viet Nam 1976 Vietnam, Democratic Republic of
# 4 Viet Nam 1977 Vietnam
# 5 Viet Nam 1978 Vietnam
您需要做的就是确保在定义数据框时将它们设置为 stringsAsFactors=F
,即 (df <- data.frame(countryname, year, stringsAsFactors=F)
)。并取出return
命令:
country_panel <- function(x, y) {
ifelse(cnames$time < y,
cnames[match(x, cnames$country),]$panel,
cnames[match(x, cnames$country),]$standardize
)
}
其背后的原因是 return
一旦函数被调用就会停止其运行。因此,您的数据框由单个值输出填充。这就是为什么它们都是一样的。