从另一个变量在数据集中创建新变量
我想知道如何从数据集的变量中创建另一个变量,该变量从另一个变量中包含的值中获取值。也就是说,我有一个名为“age”的可用变量,其中包含人们年龄的整数值。因此,我想在此数据集中创建一个名为“教育”的变量,这样如果年龄小于 7,教育的值为“小学教育”。如果年龄在 7 至 12 岁之间,则教育的值为“中等教育”。知道我该怎么做吗?
我尝试做类似以下的事情,但没有得到结果
if ((df$age) < 7){
df$education="primary education"
}
I would like to know how from the variables of a dataset I can create another variable that takes values from the value contained in another variable. That is to say, I have an available variable called "age" that contains integral values with the ages of people. Therefore, I want to create a variable in this dataset called "education", so that if age is less than 7, education takes the value "primary education". If age is between 7 and 12, education takes the value "secondary education". Any idea how I can do this?
I've tried to do something like the following but I do not get results
if ((df$age) < 7){
df$education="primary education"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用基本 R,您可以使用
ifelse
命令来执行逐元素条件:您可以嵌套
ifelse
语句来获取更多级别(尽管不是很优雅):With base R, you can use the
ifelse
command for element-wise conditions:You can nest
ifelse
statements to obtain more levels (although not very elegantly):这是使用
tidyverse
和更具体的dplyr
包的解决方案Here is a solution using the
tidyverse
and more specifc thedplyr
package