现在的输出如下所示
[2] "DOYLESTOWN HOSPITAL | PA | 10.4"
[3] "GLENDALE ADVENTIST MEDICAL CENTER | CA | 10.5"
[4] "AVERA HEART HOSPITAL OF SOUTH DAKOTA LLC | SD | 10.5"
[5] "WATERBURY HOSPITAL | CT | 10.6"
[6] "MAIN LINE HOSPITAL LANKENAU | PA | 10.7"
我使用这段代码来生成列表
for (x in df){
paste(df$name,df$state, df$outcome, sep = " | ")
}
现在我想创建一个数据框架,并使用以下数据填充它
预期输出
hospital state rank
D W MCMILLAN MEMORIAL HOSPITAL AL 10
ARKANSAS METHODIST MEDICAL CENTER AR 14
类(df) [1]
"data.frame">
我们可以使用:
mytext <- c("DOYLESTOWN HOSPITAL | PA | 10.4","GLENDALE ADVENTIST MEDICAL CENTER | CA | 10.5")
splitted <- strsplit(mytext, split="\|")
hospital <- sapply(splitted, function(x) x[[1]])
state <- sapply(splitted, function(x) x[[2]])
rank <- sapply(splitted, function(x) x[[3]])
result <- data.frame(hospital, state, rank)
> result
hospital state rank
1 DOYLESTOWN HOSPITAL PA 10.4
2 GLENDALE ADVENTIST MEDICAL CENTER CA 10.5
然而,如果你已经有了df
中可用的数据,我真的不明白这一点。当然,如果您只是在语法之后,它是有意义的。
下次请确保您的数据可重复。
在base R
中,如果正确指定分隔符,则可以使用read.csv/read.table
read.csv(text = gsub("\s+\|\s+", ",", str1),
header = FALSE, col.names = c("hospital", "state", "rank"))
hospital state rank
1 DOYLESTOWN HOSPITAL PA 10.4
2 GLENDALE ADVENTIST MEDICAL CENTER CA 10.5
数据str1 <- c("DOYLESTOWN HOSPITAL | PA | 10.4",
"GLENDALE ADVENTIST MEDICAL CENTER | CA | 10.5"
)