R 使用新标签创建新列
将数字列中的数字:R 使用新标签创建新列,r,R,将数字列中的数字: data <- data.frame(ci = c(name = c("sth", "another", "more"), numbers = c(1,2,7)) 我们可以在时在case\u中创建一个逻辑表达式来实现这一点。如果没有一个条件为真,默认情况下,它返回NA library(dplyr) df %>% mutate(match = case_when(numbers %in% c(1,
data <- data.frame(ci = c(name = c("sth", "another", "more"), numbers = c(1,2,7))
我们可以在时在case\u中创建一个逻辑表达式来实现这一点。如果没有一个条件为真,默认情况下,它返回
NA
library(dplyr)
df %>%
mutate(match = case_when(numbers %in% c(1, 2) ~ 'google',
numbers == 7 ~ 'yahoo'))
数据
df我们可以通过在case\u中创建逻辑表达式来实现这一点。如果没有一个条件为真,默认情况下,它返回NA
library(dplyr)
df %>%
mutate(match = case_when(numbers %in% c(1, 2) ~ 'google',
numbers == 7 ~ 'yahoo'))
数据
dfdata$match[与(数据,ci%在%c(1,2))匹配]data$match[与(数据,ci%在%c(1,2))匹配]请检查示例。没有7
,如何创建“雅虎”@akrun谢谢,更新!您的数据结构存在问题。我想你的意思是df Trydf%>%mutate(match=case_when(数字%in%1:2~'google',数字==7~'yahoo'))
@akrun谢谢你能用这样的c(1,2)来代替1:2吗?请检查你的例子。没有7
,如何创建“雅虎”@akrun谢谢,更新!您的数据结构存在问题。我猜你的意思是df Trydf%>%mutate(match=case_when(数字%in%1:2~'google',数字==7~'yahoo'))
@akrun谢谢你能用这样的c(1,2)来代替1:2吗?
df <- data.frame(ci = c("sth", "another", "more"), numbers = c(1, 2, 7))
data$match[with(data, ci %in% c(1,2))] <- 'google'
data$match[with(data, ci %in% c(7))] <- 'yahoo'
data
# ci match
#name1 sth <NA>
#name2 another <NA>
#name3 more <NA>
#numbers1 1 google
#numbers2 2 google
#numbers3 7 yahoo