首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的值标签?

R中的值标签?
EN

Stack Overflow用户
提问于 2020-02-17 05:04:12
回答 2查看 398关注 0票数 3

感谢到目前为止我在这里得到的所有支持。非常感谢!

我正在尝试将值标签添加到R变量中。

例如,我目前在SPSS中有以下内容:

代码语言:javascript
复制
VARIABLE LABELS band_age_3 'Three-way banded age group'.

VALUE LABELS band_age_3
               1 '1-29'
               2 '30-49'
               3 '50-59'

FREQUENCIES band_age_3.

这是最好的方法吗:

代码语言:javascript
复制
2019data$band_age_3 <- factor(2019data$band_age_3,
levels = c(1,2,3),
labels = c("1-29", "30-49", "50-59"))

就像所有与R相关的事情一样,有多种方式可以做事情,我想知道这是否是最合适的。

干杯。

EN

回答 2

Stack Overflow用户

发布于 2020-09-20 03:21:19

要在R中使用SPSS风格管理变量元数据,可以使用expss包。

使用一个可重现的例子:

代码语言:javascript
复制
df <- data.frame(band_age_3  = c(1, 3, 1, 2, 3), 
                 sex = c(1, 2, 2, 1, 1),
                 weight = c(50, 20, 30, 40, 5))

library(expss)
library(labelled)

=======================================================================
# Setting variable metadata
=======================================================================

# Set variable labels
var_lab(df$band_age_3) <- "Three-way banded age group" 
var_lab(df$sex) <- "Gender" 


# Get variable labels
var_lab(df$band_age_3)
[1] "Three-way banded age group"
var_lab(df$sex)
[1] "Gender"


# Set value labels
val_lab(df$band_age_3) <- make_labels("1 1-29
                                       2 30-49
                                       3 50-59")

val_lab(df$sex) <- make_labels("1 Men
                                2 Women")

# Get variable labels
val_lab(df$band_age_3)
 1-29 30-49 50-59 
    1     2     3 

val_lab(df$sex)
  Men Women 
    1     2 

=======================================================================
Frequencies and crosstabulations
=======================================================================


# Frequencies
=================================  

  # Unweighted
  fre(df$band_age_3)
 
 
 | Three-way banded age group | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
 | -------------------------- | ----- | ------------- | ------- | ------------ | ----------------------- |
 |                       1-29 |     2 |            40 |      40 |           40 |                      40 |
 |                      30-49 |     1 |            20 |      20 |           20 |                      60 |
 |                      50-59 |     2 |            40 |      40 |           40 |                     100 |
 |                     #Total |     5 |           100 |     100 |          100 |                         |
 |                       <NA> |     0 |               |       0 |              |                         |

  
  # Weighted 
  fre(df$band_age_3, weight = df$weight)

 | Three-way banded age group | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
 | -------------------------- | ----- | ------------- | ------- | ------------ | ----------------------- |
 |                       1-29 |    80 |          55.2 |    55.2 |         55.2 |                    55.2 |
 |                      30-49 |    40 |          27.6 |    27.6 |         27.6 |                    82.8 |
 |                      50-59 |    25 |          17.2 |    17.2 |         17.2 |                   100.0 |
 |                     #Total |   145 |         100.0 |   100.0 |        100.0 |                         |
 |                       <NA> |     0 |               |     0.0 |              |                         |


# Crosstabs
=================================    
  
  # Weighted 
  
      # Count
      cro(df$band_age_3, list(total(),df$sex), weight = df$weight, total_statistic = "w_cases")

 |                            |                   | #Total | Gender |       |
 |                            |                   |        |    Men | Women |
 | -------------------------- | ----------------- | ------ | ------ | ----- |
 | Three-way banded age group |              1-29 |     80 |     50 |    30 |
 |                            |             30-49 |     40 |     40 |       |
 |                            |             50-59 |     25 |      5 |    20 |
 |                            | #Total wtd. cases |    145 |     95 |    50 |
      
      # Row percentages
      cro_rpct(df$band_age_3, list(total(),df$sex), weight = df$weight, total_statistic = "w_rpct")

 |                            |                  | #Total | Gender |       |
 |                            |                  |        |    Men | Women |
 | -------------------------- | ---------------- | ------ | ------ | ----- |
 | Three-way banded age group |             1-29 |    100 |   62.5 |  37.5 |
 |                            |            30-49 |    100 |  100.0 |       |
 |                            |            50-59 |    100 |   20.0 |  80.0 |
 |                            | #Total wtd. rpct |    100 |   65.5 |  34.5
      
      # Column percentages
      cro_cpct(df$band_age_3, list(total(),df$sex), weight = df$weight, total_statistic = "w_cpct")  
      
 |                            |                  | #Total | Gender |       |
 |                            |                  |        |    Men | Women |
 | -------------------------- | ---------------- | ------ | ------ | ----- |
 | Three-way banded age group |             1-29 |   55.2 |   52.6 |    60 |
 |                            |            30-49 |   27.6 |   42.1 |       |
 |                            |            50-59 |   17.2 |    5.3 |    40 |
 |                            | #Total wtd. cpct |  100.0 |  100.0 |   1

      # Total percentages 
      cro_tpct(df$band_age_3, list(total(),df$sex), weight = df$weight, total_statistic = "w_tpct")
    
 |                            |                  | #Total | Gender |       |
 |                            |                  |        |    Men | Women |
 | -------------------------- | ---------------- | ------ | ------ | ----- |
 | Three-way banded age group |             1-29 |   55.2 |   34.5 |  20.7 |
 |                            |            30-49 |   27.6 |   27.6 |       |
 |                            |            50-59 |   17.2 |    3.4 |  13.8 |
 |                            | #Total wtd. tpct |  100.0 |   65.5 
    

# Unweighted

    # Count
    cro(df$band_age_3, list(total(),df$sex), total_statistic = "u_cases")

 |                            |              | #Total | Gender |       |
 |                            |              |        |    Men | Women |
 | -------------------------- | ------------ | ------ | ------ | ----- |
 | Three-way banded age group |         1-29 |      2 |      1 |     1 |
 |                            |        30-49 |      1 |      1 |       |
 |                            |        50-59 |      2 |      1 |     1 |
 |                            | #Total cases |      5 |      3 |     2 |
    
    # Row percentages
    cro_rpct(df$band_age_3, list(total(),df$sex), total_statistic = "u_cases")

 |                            |              | #Total | Gender |       |
 |                            |              |        |    Men | Women |
 | -------------------------- | ------------ | ------ | ------ | ----- |
 | Three-way banded age group |         1-29 |    100 |     50 |    50 |
 |                            |        30-49 |    100 |    100 |       |
 |                            |        50-59 |    100 |     50 |    50 |
 |                            | #Total cases |      5 |      3 |     2 |
    
    # Column percentages
    cro_cpct(df$band_age_3, list(total(),df$sex), total_statistic = "u_cases") 

 |                            |              | #Total | Gender |       |
 |                            |              |        |    Men | Women |
 | -------------------------- | ------------ | ------ | ------ | ----- |
 | Three-way banded age group |         1-29 |     40 |   33.3 |    50 |
 |                            |        30-49 |     20 |   33.3 |       |
 |                            |        50-59 |     40 |   33.3 |    50 |
 |                            | #Total cases |      5 |    3.0 |     2 | 
    
    # Total percentages 
    cro_tpct(df$band_age_3, list(total(),df$sex), total_statistic = "u_cases") 

 |                            |              | #Total | Gender |       |
 |                            |              |        |    Men | Women |
 | -------------------------- | ------------ | ------ | ------ | ----- |
 | Three-way banded age group |         1-29 |     40 |     20 |    20 |
 |                            |        30-49 |     20 |     20 |       |
 |                            |        50-59 |     40 |     20 |    20 |
 |                            | #Total cases |      5 |      3 |   
票数 2
EN

Stack Overflow用户

发布于 2020-02-17 05:08:57

我们可以使用命名向量进行替换

代码语言:javascript
复制
setNames(c("1-29", "30-49", "50-59"), 1:3)[as.character(`2019data`$band_age_3)]

使用可重现的示例

代码语言:javascript
复制
unname(setNames(c("1-29", "30-49", "50-59"), 1:3)[as.character(c(1, 3, 2, 1, 3))])
#[1] "1-29"  "50-59" "30-49" "1-29"  "50-59"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60253192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档