Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用以下方法dplyr:
dplyr
假设您有data.frame y并且您想根据 的值创建一个新变量var1
data.frame
y
var1
y <- data.frame(var1 = rnorm(100)) y$var2 <- 0 y$var2[y$var1 > 0.5] <- 1
dplyr是否可以使用'smutate和来做到这一点filter?
mutate
filter
尝试mutate:
> y <- data.frame(var1 = (-2):2) > y %>% mutate(var2 = as.numeric(var1 > 0.5)) var1 var2 1 -2 0 2 -1 0 3 0 0 4 1 1 5 2 1
更新: dplyr 现在用来%>%代替%.%
%>%
%.%