大佬教程收集整理的这篇文章主要介绍了为字符变量创建标签列,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有这个只有字符变量的示例数据集。
dxe1<-c("W07XXXA","NulL","3")
dxe1_poa<-c("Y","N")
dxe2<-c("NulL","NulL")
dxe2_poa<-c("NulL","NulL")
df3 <- data.frame(dxe1,dxe1_poa,dxe2,dxe2_poa)
我想标记变量,所以我为它们创建了一个标签向量:
var.labels = c(dxe1="External Cause of Injury Diagnosis 1",dxe1_poa="External Cause of Injury Diagnosis 1 - Present on Admission",dxe2="External Cause of Injury Diagnosis 2",dxe2_poa="External Cause of Injury Diagnosis 2 - Present on Admission")
label(df3) = as.List(var.labels[match(names(df3),names(var.labels))])
label(df3)
我的目标是创建一个类似于下面的表,即我想要一个标签列,给出变量描述。我只希望缺失的观察结果显示为统计数据,而不是 min、max、mean、sd。他们应该只是不适用。如下表所示。
我正在尝试使用以下代码集:
df3 <- Filter(is.character,df3)
Variables <- names(df3)
Label <- label(df3)
Missing <- sapply(df3,function(X) sum(is.na(X)))
Type <- sapply(df3,function(X) {tmp <- class(X);if(length(X) > 1) tmp[2] else tmp[1]})
Min <- sapply(df3,function(X) min(x,na.rm = TRUE))
Max <- sapply(df3,function(X) max(x,na.rm = TRUE))
SD <- sapply(df3,function(X) format(round(sd(x,na.rm=TRUE),2),nsmall = 2))
Mean <- sapply(df3,function(X) format(round(R_376_11845@ean(x,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),"latex")
但是,使用上面的代码集,仍然显示平均值和标准差的统计数据。我想让它们像上表一样显示为“n.a”。有什么建议么?另外,我在字符形式中出现了最小值和最大值。我只想显示数字表格。
你可以试试这个:
df3 <- Filter(is.character,df3)
Variables <- names(df3)
Label <- label(df3)
Missing <- sapply(df3,function(X) sum(is.na(X)))
Type <- sapply(df3,function(X) {tmp <- class(X);if(length(X) > 1) tmp[2] else tmp[1]})
Min <- 'n.a'
Max <- 'n.a'
SD <- 'n.a'
Mean <- 'n.a'
#To get the Latex table for the rows
knitr::kable(data.frame(Variables,Label,Missing,Type,Min,Max,Mean,SD,row.names = NULL),"latex")
以上是大佬教程为你收集整理的为字符变量创建标签列全部内容,希望文章能够帮你解决为字符变量创建标签列所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。