我在R中有一个数据集,其中有一个包含0-1个月、8-9个月等的列。我想将此列编码为一个具有月数的数值变量。例如,不是8-9个月,而是9个月。谢谢你的帮助和评论。
发布于 2012-08-22 06:50:58
一举
a <- c("0-1 month", "8-9 months")
as.integer(gsub("^[[:digit:]]+-([[:digit:]]+) month[s]*", "\\1", a))
发布于 2012-08-22 07:41:09
使用car
包中的recode
函数。
library(car)
a <- c("0-1 month", "8-9 months")
recode(a, '"0-1 month" = 1; "8-9 months" = 2')
https://stackoverflow.com/questions/12064109
复制相似问题