通过参数R获取均值列表

时间:2014-07-27 14:37:00

标签: r

我的数据如下:

> head(chf)
  Admit.Day.of.Week Type.of.Admission          Patient.Disposition
1               SAT         Emergency         Skilled Nursing Home
2               FRI          Elective            Home or Self Care
3               FRI         Emergency Home w/ Home Health Services
4               MON         Emergency         Skilled Nursing Home
5               THU         Emergency            Home or Self Care
6               WED         Emergency         Skilled Nursing Home
  mean_los_dispo
1       8.553525
2       4.224193
3       5.789052
4       8.553525
5       4.224193
6       8.553525

我使用以下命令获取标记为mean_los_dispo

的列
# Mean LOS for each patient disposition
chf$mean_los_dispo <- ave(chf$Length.of.Stay, chf$Patient.Disposition,
                         FUN = mean)

我想要做的是设置一个变量来保存四个不同配置中每个配置的mean_los_dispo的值,例如

SNH = 8.553525
HSC = 4.224193
...

我该怎么做呢?我希望最终能够使用paste或类似的东西将信息放在图表的标题中。

1 个答案:

答案 0 :(得分:1)

您可以使用粘贴。例如,我创建了两个变量,一个是数字(所以你的意思),另一个是字符(所以你的处置),然后我用paste来连接它们:

a<-c(1,2,3,4,5)
b<-c("a","b","c","d","e")

strs<-paste(b," = ",as.character(a),sep="")

这会产生:

[1] "a = 1" "b = 2" "c = 3" "d = 4" "e = 5"

在您的情况下,您可以执行以下操作:

unique(paste(chf$Patient.Disposition," = ",as.character(chf$mean_los_dispo),sep=""))

独特的将摆脱所有重复。