首页 > 解决方案 > 为什么在粘贴输出的末尾添加“.NULL”?

问题描述

我一直在尝试访问用户从数据框中给出的特定列。为此,我使用了“粘贴”功能来连接用户给定的参数“结果”。现在,结果可以有三个可能的值,即“心脏病发作”、“心力衰竭”、“肺炎”。我的目的是将输入的第一个字母大写,然后将其粘贴到列名以完成它。

rankhospital <- function(outcome) {

  # Here the outcome can be  c('heart attack','heart failure','pneumonia')
  b <- strsplit(outcome,' ') # For the two outcomes that have a space in between i.e. 'heart attack' & 'heart failure', I need to capitalise each of the first letter.
  c <- b[1]
  d <- b[2]
# If the outcome is 'pneumonia' then the d variable will be NULL
  if (!is.null(d)){
    e <- paste(toupper(substr(c, 1, 1)), substr(c, 2, nchar(c)), sep="")
    f <- paste(toupper(substr(d, 1, 1)), substr(d, 2, nchar(d)), sep="")
    g <- paste("Hospital.30.Day.Death..Mortality..Rates.from.",e,".",f,sep='')
  }
  else{
    e <- paste(toupper(substr(c, 1, 1)), substr(c, 2, nchar(c)), sep="")
    g <- paste("Hospital.30.Day.Death..Mortality..Rates.from.",e,sep='')
  }
  g
}

对于“肺炎”作为结果,我得到如下输出:“Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia.NULL” 由于这个 .NULL 最终我无法访问此列并且它总是返回 NULL

对于“心脏病发作”或“心力衰竭”,输出类似于:“Hospital.30.Day.Death..Mortality..Rates.from.C(\"heart\", \"attack\").NULL “在这里,我似乎无法理解为什么它甚至不将字母大写

标签: r

解决方案


推荐阅读