将值标签分配给变量的整形

时间:2017-08-15 09:56:30

标签: r

我有一个数据框“CB”,其中包含“性别”列,其中包含两个变量“男性”和“女性”。 我希望“男性”获得值0,“女性”获得值1.

我这样做了:

CB$gender <- ifelse(CB$gender == "Male", 0, ifelse(CB$gender == "Female", 1, 
999))

当我查看专栏时,我得到了这个结果:

head(CB$gender, 3)
[1] 1 0 0

没关系!

当我制作一张桌子时,我得到了这个结果:

table(CB$gender)

  0    1 
3555 3488 

我希望0表示“男性”,1表示“女性”

为了更好地理解:

在SPSS中,可以将值0分配给“Male”,将值1分配给“Female”。当我打印一张桌子时,它看起来像这样:

Male Female
3555 3488 

但该栏目如下:

gender
0
1
1
0

这就是我想要的。

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用factor将变量转换为因子并标记它:

CB <- data.frame(gender= c("Male", "Female", "Male", "Male", "Female", "Female", "Male", "Male"))
CB$gender <- ifelse(CB$gender == "Male", 0, ifelse(CB$gender == "Female", 1, 999))
str(CB)
#'data.frame':  8 obs. of  1 variable:
# $ gender: num  0 1 0 0 1 1 0 0
CB$gender <- factor(CB$gender ,
                labels=c("Male", "Female"))
str(CB)
#'data.frame':  8 obs. of  1 variable:
# $ gender: Factor w/ 2 levels "Male","Female": 1 2 1 1 2 2 1 1
table(CB$gender)
#Male Female 
#   5      3 

注意:R通过转换为因子从1分配级别(在这种情况下为1和2)

或者,您可以使用包lfactors

library(lfactors)
CB <- data.frame(gender= c("Male", "Female", "Male", "Male", "Female", "Female", "Male", "Male"))
CB$gender <- ifelse(CB$gender == "Male", 0, ifelse(CB$gender == "Female", 1, 999))
CB$gender <- lfactor(CB$gender,
                 levels=0:1,
                 labels=c("Male", "Female"))
print(CB$gender)
#[1] Male   Female Male   Male   Female Female Male   Male  
#Levels: Male Female
#Numeric levels: 0 1 
table(CB$gender)
#  Male Female 
#     5      3 
attributes(CB$gender)$llevels
#[1] 0 1

最接近SPSS类比的另一个选项是使用包labelledsjmisc,如下所示:

library(labelled)
library(sjmisc) #access to function to_label()
CB <- data.frame(gender= c("Male", "Female", "Male", "Male", "Female", "Female", "Male", "Male"))
CB$gender <- ifelse(CB$gender == "Male", 0, ifelse(CB$gender == "Female", 1, 999))
val_labels(CB$gender) <- c("Male" = 0, "Female" = 1)
print(CB$gender)
#<Labelled double>
#[1] 0 1 0 0 1 1 0 0
#Labels:
# value  label
#     0   Male
#     1 Female
table(to_label(CB$gender))
#  Male Female 
#     5      3 

此外,您可以使用包sjmiscsjlabelled的组合。

library(sjmisc) #access to function to_label()
library(sjlabelled) #function set_labels()
CB <- data.frame(gender= c("Male", "Female", "Male", "Male", "Female", "Female", "Male", "Male"))
CB$gender <- ifelse(CB$gender == "Male", 0, ifelse(CB$gender == "Female", 1, 999))
CB$gender <- set_labels(CB$gender, labels=c("Male", "Female"))
print(CB$gender)
#[1] 0 1 0 0 1 1 0 0
#attr(,"labels")
#  Male Female 
#     0      1 
table(to_label(CB$gender))
#  Male Female 
#     5      3 

函数to_label()与包lfactorslabelledsjlabelled的工作方式类似。