r语言重新编码体重
发布网友
发布时间:2023-07-15 23:35
我来回答
共1个回答
热心网友
时间:2024-08-24 04:30
重编码涉及根据同一个变量和/或其他变量的现有值创建新值的过程,如将符合某个条件的值重新赋值等,这里主要介绍两种常见的方法:
#第一种方法
per <- data.frame(name = c("张三","李四","王五","赵六"),
age = c(23,45,34,1000))
per
per$age[per$age == 1000] <- NA #设置缺失值
per$age1[per$age < 30] = "young" #生成新变量
per$age1[per$age >= 30 & per$age<50] <- "middle age"
per
#第二种方法
per <- data.frame(name = c("张三","李四","王五","赵六"),
age = c(23,45,34,1000))
per <- within(per,{
age1 <- NA
age1[age < 30] <- "young"
age1[age>=30 & age<50] <- "middle age"
})
per
热心网友
时间:2024-08-24 04:30
重编码涉及根据同一个变量和/或其他变量的现有值创建新值的过程,如将符合某个条件的值重新赋值等,这里主要介绍两种常见的方法:
#第一种方法
per <- data.frame(name = c("张三","李四","王五","赵六"),
age = c(23,45,34,1000))
per
per$age[per$age == 1000] <- NA #设置缺失值
per$age1[per$age < 30] = "young" #生成新变量
per$age1[per$age >= 30 & per$age<50] <- "middle age"
per
#第二种方法
per <- data.frame(name = c("张三","李四","王五","赵六"),
age = c(23,45,34,1000))
per <- within(per,{
age1 <- NA
age1[age < 30] <- "young"
age1[age>=30 & age<50] <- "middle age"
})
per