【问题标题】:Creating a Label column in R for the unnamed variables在 R 中为未命名的变量创建标签列
【发布时间】:2021-03-15 23:37:32
【问题描述】:

我想创建一个名为 Label 的列以显示在我的汇总统计信息中。例如,如下表所示。

假设我有这个数据框:

df <- data.frame(age_years = c(33, 11, 45, 67, 8, 99), sex = factor(c(0, 1, 1, 0, 0, 0)))

> df

      age_years       sex
    1          33          0
    2          11          1
    3          45          1
    4          67          0
    5          8           0
    6          99          0

我尝试使用Hmisc 包。

library(Hmisc)

var.labels = c(age_years="Age in Years", sex="Sex")

label(df) = as.list(var.labels[match(names(df), names(var.labels))])

对于汇总表,我使用了以下代码:

Variables <- names(df)
Label <- label(df)
Missing <- sapply(df, function(x) sum(is.na(x)))
Type <- sapply(df, function(x) class(x))
Min <- sapply(df, function(x) min(x, na.rm = TRUE))
Max <- sapply(df, function(x) max(x, na.rm = TRUE))
SD <- sapply(df, function(x) format(round(sd(x, na.rm=TRUE), 2), nsmall = 2))
Mean <- sapply(df, function(x) format(round(mean(x, na.rm=TRUE), 2), nsmall = 2))
#To get the Latex table for the rows 
knitr::kable(as.data.frame(cbind(Variables, Label, Missing, Type, Min, Max, Mean, SD), row.names = FALSE), "latex")

我没有使用上述代码获得名为“标签”的列。我想在汇总统计表的标签列中将变量 age_years 标记为“年龄”,而性别变量也是如此。我从 R 得到这个乳胶输出。

   \begin{tabular}{l|l|l|l|l|l|l|l}
\hline
Variables & Missing & age\_years & sex & Min & Max & Mean & SD\\
\hline
age\_years & 0 & labelled & labelled & 0 & 104 & 54.94 & 20.06\\
\hline
sex & 0 & numeric & numeric & 1 & 2 & 1.46 & 0.50\\
\hline
\end{tabular}

我的 Latex 表看起来不像上面那个:

我不确定为什么 age_years 和 sex 显示为列。我想摆脱这些可变列。

我觉得这部分代码有问题Label &lt;- label(df)

【问题讨论】:

    标签: r dataframe label


    【解决方案1】:

    class 中可以有多个值,例如:

    class(df$age_years)
    #[1] "labelled" "numeric" 
    

    将这些类组合成一个逗号分隔的字符串。

    toString(class(df$age_years))
    #[1] "labelled, numeric"
    

    或者,如果您想选择第二类(“数字”),您可以使用if 条件。

    if(length(class(df$age_years) > 1)) class(df$age_years)[2] else class(df$age_years)[1]
    #[1] "numeric"
    

    使用if 方法完成代码 -

    df <- Filter(is.numeric, df)
    Variables <- names(df)
    Label <- label(df)
    Missing <- sapply(df, function(x) sum(is.na(x)))
    Type <- sapply(df, function(x) {tmp <- class(x);if(length(x) > 1) tmp[2] else tmp[1]})
    Min <- sapply(df, function(x) min(x, na.rm = TRUE))
    Max <- sapply(df, function(x) max(x, na.rm = TRUE))
    SD <- sapply(df, function(x) format(round(sd(x, na.rm=TRUE), 2), nsmall = 2))
    Mean <- sapply(df, function(x) format(round(mean(x, na.rm=TRUE), 2), nsmall = 2))
    #To get the Latex table for the rows 
    knitr::kable(data.frame(Variables, Label, Missing, Type, Min, Max, Mean, SD, row.names = NULL))
    
    |Variables |Label        | Missing|Type    | Min| Max|Mean  |SD    |
    |:---------|:------------|-------:|:-------|---:|---:|:-----|:-----|
    |age_years |Age in Years |       0|numeric |   8|  99|43.83 |34.82 |
    

    【讨论】:

    • 感谢您的回答。但是,我得到的输出没有标签。对于 age_years 变量,标签作为变量本身的名称出现,对于性别也是如此。我想在标签列中分别将它们标记为“年龄”和“性别”。基本上我在这里尝试的var.labels = c(age_years="Age in Years", sex="Sex")
    • Label 列基本上只是为了表示变量描述。我想创建一个类似第一个表的表。
    • 我在名为Label 的列中看到了标签。我已经用我得到的输出更新了答案。您不想/更改输出的哪一部分?
    • 是的,我看到输出中的标签列显示“年龄”,这正是我要寻找的。但是,在“类型”列中,我只想要一种类型,即“数字”。不想将“标记”作为一种类型。我不确定是否应该在 Type 下使用 string toString(class(x))
    • 为此,您需要使用if/else 方法。我已经更新了答案以包含它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2014-09-29
    • 2021-07-30
    相关资源
    最近更新 更多