将代码从基数R转换为dplyr,特别是添加变量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将代码从基数R转换为dplyr,特别是添加变量相关的知识,希望对你有一定的参考价值。
我正在尝试向数据集“ birth_data”添加一个名为“ state_color”的新变量。我想使用dplyr简化代码,但不确定如何转换。我知道在R底下看起来像这样:
red <- c("AK","ID","KS","NE","ND","OK","UT","WY","TX","AL","MS","SC","MT","GA","MO","LA","TN","AK","KY","AZ","WV")
purple <- c("NC","VA","FL","OH","CO","NV","IN","IA","NM")
blue <- c("NH","PA","CA","MI","IL","MA","DE","NJ","CT","VT","ME","WA","OR","WI","NY","MA","RI","HI","MN","DC")
#assigning states to their respective color
birth_data$state_color[birth_data$state %in% red] <- "red"
birth_data$state_color[birth_data$state %in% purple] <- "purple"
birth_data$state_color[birth_data$state %in% blue] <- "blue"
head(birth_data)
我尝试对dplyr进行相同操作
red <- c("AK","ID","KS","NE","ND","OK","UT","WY","TX","AL","MS","SC","MT","GA","MO","LA","TN","AK","KY","AZ","WV")
purple <- c("NC","VA","FL","OH","CO","NV","IN","IA","NM")
blue <- c("NH","PA","CA","MI","IL","MA","DE","NJ","CT","VT","ME","WA","OR","WI","NY","MA","RI","HI","MN","DC")
#assigning states to their respective color
birth_data %>%
mutate(state_color <- c("red","purple","blue"))
但随后得到错误
错误:列
state_color <- c("red", "purple", "blue")
的长度必须为1103629(行数)或一个,而不是3
我在做什么错?
答案
您想从数据集开始,进行变异以创建一个新列,然后使用“ case_when”。如果所有情况均失败,则使用TRUE值。
red <- c("AK","ID","KS","NE","ND","OK","UT","WY","TX","AL","MS","SC","MT","GA","MO","LA","TN","AK","KY","AZ","WV")
purple <- c("NC","VA","FL","OH","CO","NV","IN","IA","NM")
blue <- c("NH","PA","CA","MI","IL","MA","DE","NJ","CT","VT","ME","WA","OR","WI","NY","MA","RI","HI","MN","DC")
birth_data %>%
mutate(state_color =case_when(
state %in% red ~ "red",
state %in% purple ~ "purple",
state %in% blue ~ "blue",
TRUE ~ "no color"
))
另一答案
尝试一下:
出生数据%>%mutate(state_color = if_else(状态%in%红色,“红色”,if_else(状态%in%紫色,“紫色”,“蓝色”))
另一答案
如果您要从基数扩展,也应该查看data.table:>
library(data.table)
dt_states <- data.table(state = state.abb)
dt_states[state %in% red, state_color := 'red']
dt_states[state %in% blue, state_color := 'blue']
dt_states[state %in% purple, state_color := 'purple']
dt_states
以上是关于将代码从基数R转换为dplyr,特别是添加变量的主要内容,如果未能解决你的问题,请参考以下文章
使用 httr 将 curl 命令转换为 R(特别是 '--data-binary @')
R语言dplyr包的mutate函数将列添加到dataframe中或者修改现有的数据列:基于条件判断创建布尔型指示变量将异常离散编码转化为NA值
从 int 获取单个数字以在 C/C++ 中进行基数排序的最佳方法
R语言dplyr包使用recode函数进行数据列内容编码转换实战:类似于pandas中的map函数(例如,将内容从字符串映射到数值)