我希望函数只提供一个字符值作为输出,但是当read.csv采用colClasses的默认值时,会有一个带字符值的附加输出。为什么当我指定colClasses =“character”时我的函数运行良好但不是这样?
top <- function(state, outcome) {
a <- read.csv("outcome-of-care-measures.csv")
if (outcome == "heart attack") {
data <- a[a[[7]]==state,]
x <- suppressWarnings(as.numeric(data[[11]]))
y <- min(x, na.rm = TRUE)
index <- which(x == y)
z <- data[index,2]
z1 <- sort(z)
print(z1[1])
}
}
输出(包含关于级别的不需要的信息)是
> top("TX", "heart attack")
[1] CYPRESS FAIRBANKS MEDICAL CENTER
4510 Levels: ABBEVILLE AREA MEDICAL CENTER ... ZUNI COMPREHENSIVE COMMUNITY HEALTH CENTER
答案 0 :(得分:3)
因素(R用于存储分类变量的数据类型)随之携带可能的级别,默认情况下会打印这些级别。有多种解决方案:
colClasses
; stringsAsFactors=FALSE
print(as.character(z1[1]))
print(z1[1],max.levels=0)