为什么在粘贴输出的末尾添加“ .NULL”?

时间:2019-10-15 20:34:34

标签: r

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

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

对于“心脏病发作”或“心脏衰竭”,输出类似于: “医院30.Day.Death..Morality..Rates.from.C(\“ heart \”,\“ attack \”)。NULL“ 在这里,我似乎无法理解为什么它甚至都没有大写

0 个答案:

没有答案