简体   繁体   English

如何在R中使用非数字数据创建虚拟变量?

[英]How do I create a dummy variable in R with non-numeric data?

I have a column of data listing various education levels from census data, they vary from "HS grad" to "Doctorate" to "9th" for the highest level of education the person received. 我有一列数据,列出了人口普查数据中的各种教育水平,从“ HS等级”到“博士学位”再到“ 9th”,代表该人获得的最高教育水平。

I want to create a dummy variable for whether someone is educated or not, and in the "educated=YES" category I want it to list the respondents with college or vocational school backgrounds. 我想为某人是否受过教育创建一个虚拟变量,在“ educated = YES”类别中,我希望它列出具有大学或职业学校背景的受访者。 I've tried using "&" but it won't group them. 我尝试使用“&”,但不会将它们分组。

C <- data.frame(educated=census$education=="Assoc-acdm" & "Assoc voc" & "Bachelors" & "Doctorate" & "Masters" & "Prof-school")

Anyone know how do group non-numeric responses? 有人知道如何对非数字响应进行分组吗?

Instead, you would want: 相反,您需要:

C <- data.frame(educated=(census$education %in% c("Assoc-acdm", 
              "Assoc voc", "Bachelors", "Doctorate", "Masters", "Prof-school")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取 R 中另一个变量的变量总数? 这两个变量都是非数字的 - How do I get the aggregate number of a variable against another variable in R? Both these variables are non-numeric r-如何使用非数字数据分类? - r - How to classify with non-numeric data? 如何使用非数字时间数据在 r 中绘制直方图? - how to draw histogram graph in r with non-numeric time data? 如何修复我的自定义函数以处理包含非数字变量的数据帧 - How do I fix my custom function to work on dataframes that include non-numeric variable R中具有非数值数据的相关矩阵 - Correlation Matrix in R with Non-numeric Data 如何在 R 的值范围内创建虚拟变量? - How do I create a dummy variable within a range of values in R? 使用R将非数字数据转换为数字数据 - transform non-numeric data to numeric data with R 如何在 R 中的用户定义函数中拟合线性模型; 指定响应变量时错误的非数字参数? - How can I fit a linear model inside a user defined function in R; error non-numeric argument when specifying response variable? 如何将非数值变量列转换为两个数值变量列? - How can I convert a non-numeric variable column into two numeric variable columns? 为R中的数字变量中的缺失值创建虚拟 - Create dummy for missing values in numeric variable in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM