R 从字符字段中重新编码丢失的数据

R 从字符字段中重新编码丢失的数据,r,R,注意:标题可能有误导性。如果你理解我的问题,并想一些更具描述性的东西-请改变它 我遇到了一个奇怪的情况,调查结果都是字符,而不是数字。看来R,真的不喜欢这样。假设我问了一个问题: Q. In what area do you work? East West Central North South None of the above 但受访者仅来自东部、西部和中部 dat <- rep(c("East", "West", "Central"),100) dat差不多了。您需要使用lev

注意:标题可能有误导性。如果你理解我的问题,并想一些更具描述性的东西-请改变它

我遇到了一个奇怪的情况,调查结果都是字符,而不是数字。看来R,真的不喜欢这样。假设我问了一个问题:

Q. In what area do you work? 
East
West
Central
North
South
None of the above
但受访者仅来自东部、西部和中部

dat <- rep(c("East", "West", "Central"),100)

dat差不多了。您需要使用
levels
参数:

fac1 <- factor(dat, levels=c("East","West","Central","North","South","None of the above"))
str(fac1)
 Factor w/ 6 levels "East","West",..: 1 2 3 1 2 3 1 2 3 1 ...

差不多了。您需要使用
levels
参数:

fac1 <- factor(dat, levels=c("East","West","Central","North","South","None of the above"))
str(fac1)
 Factor w/ 6 levels "East","West",..: 1 2 3 1 2 3 1 2 3 1 ...

不是专家,但这有帮助吗

fac1 <- factor(dat, levels = 
               c("East","West","Central","North","South","None of the above"))
summary(fac1)

fac1不是专家,但这有什么帮助吗

fac1 <- factor(dat, levels = 
               c("East","West","Central","North","South","None of the above"))
summary(fac1)
fac1