【问题标题】:Processing attrbutes issue处理属性问题
【发布时间】:2019-05-26 15:01:22
【问题描述】:

我正在尝试编写函数,该函数采用列属性并将它们转换为适当的字符串。

我的数据具有变量标签(列描述)、变量名称、类型(因子、数字、字符)和定义的因子水平(例如 1=yes,2=no)。我想使用生成字符串的函数,该字符串以适当的顺序包含所有信息:

Q B
Q [varname] vallab
T S #type
R yes
R no
Q E

我写了这样的函数:

WriteQ<-function(VarLab,VarName,Responses,VarType) {
  cond<-sapply(VarType, switch,
               factor="T S\n",
               numeric="T I\n",
               character="T C\n")
  mystr<-paste0("Q B\nQ [",VarName,"] ", VarLab, "\n",cond,"L0L2\n",paste("R",Responses, collapse =  "\n"),"\nQ E")
  return(mystr)
}

并试图这样称呼它:

attributes(data)$qps<-WriteQ(attributes(data)$variable.labels,
                               attributes(data)$names,
                               unlist(lapply(data, attr, "levels")),
                               unlist(lapply(data, class)))

虽然 VarLab、VarName 和 VarType 工作正常,但我的 Responses 有问题。 它在每个部分中打印整个数据框的所有级别。我应该如何为每一列分别传递级别列表?

文件结构:


structure(list(id = c(1, 2), q23 = structure(1:2, .Label = c("yes", 
"no", "don't know"), class = "factor"), gender = structure(c(1L, 
1L), .Label = c("male", "female"), class = "factor"), age = c(33, 
44)), row.names = 1:2, variable.labels = c("id", "Do you like flowers?", 
"Select gender...", "How old are you?"), class = "data.frame")

【问题讨论】:

  • 你能不能dput(data),这样我们可以得到它的副本?
  • 如果没有一个好的例子很难知道,但我认为您可能需要通过在单个列上尝试该 paste() 来处理它,直到您以您需要的方式获得它。实际上,您可能希望在将其提供给您的函数之前对其进行预处理。或者创建一个从 WriteQ 函数调用的单独函数。这样会更干净,更容易测试。
  • 什么是L0L2(除了静态字符串)?

标签: r


【解决方案1】:

这是一个镜头:

WriteQ <- function(VarLab, VarName, Responses, VarType) {
  Qs <- sprintf("Q [%s] %s", VarName, VarLab)
  Ts <- sapply(VarType, switch,
               factor="T S",
               numeric="T I",
               character="T C")
  Rs <- lapply(Responses, function(r) sprintf("R %s L0L2", r))
  QEs <- rep("Q E", length(VarLab))
  out <- Map(c, Qs, Ts, Rs, QEs)
  out
  # setNames(out, VarName)
  # setNames(lapply(out, paste, collapse = "\n"), VarName)
  # unlist(out, use.names = FALSE)
  # paste(unlist(out), collapse = "\n")
}

默认输出为:

out
# $`Q [id] id`
# [1] "Q [id] id" "T I"       "Q E"      
# $`Q [q23] Do you like flowers?`
# [1] "Q [q23] Do you like flowers?" "T S"                         
# [3] "R yes L0L2"                   "R no L0L2"                   
# [5] "R don't know L0L2"            "Q E"                         
# $`Q [gender] Select gender...`
# [1] "Q [gender] Select gender..." "T S"                        
# [3] "R male L0L2"                 "R female L0L2"              
# [5] "Q E"                        
# $`Q [age] How old are you?`
# [1] "Q [age] How old are you?" "T I"                     
# [3] "Q E"                     

其他输出格式,取决于您的需要:

setNames(out, VarName)
# $id
# [1] "Q [id] id" "T I"       "Q E"      
# $q23
# [1] "Q [q23] Do you like flowers?" "T S"                         
# [3] "R yes L0L2"                   "R no L0L2"                   
# [5] "R don't know L0L2"            "Q E"                         
# $gender
# [1] "Q [gender] Select gender..." "T S"                        
# [3] "R male L0L2"                 "R female L0L2"              
# [5] "Q E"                        
# $age
# [1] "Q [age] How old are you?" "T I"                     
# [3] "Q E"                     

setNames(lapply(out, paste, collapse = "\n"), VarName)
# $id
# [1] "Q [id] id\nT I\nQ E"
# $q23
# [1] "Q [q23] Do you like flowers?\nT S\nR yes L0L2\nR no L0L2\nR don't know L0L2\nQ E"
# $gender
# [1] "Q [gender] Select gender...\nT S\nR male L0L2\nR female L0L2\nQ E"
# $age
# [1] "Q [age] How old are you?\nT I\nQ E"

unlist(out, use.names = FALSE)
#  [1] "Q [id] id"                    "T I"                         
#  [3] "Q E"                          "Q [q23] Do you like flowers?"
#  [5] "T S"                          "R yes L0L2"                  
#  [7] "R no L0L2"                    "R don't know L0L2"           
#  [9] "Q E"                          "Q [gender] Select gender..." 
# [11] "T S"                          "R male L0L2"                 
# [13] "R female L0L2"                "Q E"                         
# [15] "Q [age] How old are you?"     "T I"                         
# [17] "Q E"                         

paste(unlist(out), collapse = "\n")
# [1] "Q [id] id\nT I\nQ E\nQ [q23] Do you like flowers?\nT S\nR yes L0L2\nR no L0L2\nR don't know L0L2\nQ E\nQ [gender] Select gender...\nT S\nR male L0L2\nR female L0L2\nQ E\nQ [age] How old are you?\nT I\nQ E"

【讨论】:

  • 感谢它在属性数量相等时有效,但并非适用于所有变量标签,并且存在响应。无论如何,谢谢它让我很好地了解了我应该去的方向。
  • 如果你用一个工作的数据样本来编辑你的问题,那么我也许可以解决任何问题。
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 2013-07-07
  • 2022-10-19
  • 2021-06-11
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多