在plyr中安排功能

时间:2018-12-17 02:52:22

标签: r dataframe dplyr

当我正在完成Coursera R编程课程的最后作业时,我的安排功能实际上并没有很好地工作,我感到很困惑

因为我已经通过拆分功能提取了状态

my_data<- outcome[,c(2,7,23)]
names(my_data)<- c("hospital","state","heart_attack","heart_failure","pneumonia")

my_data1<-lapply(split(my_data,my_data$state), data.frame)

my_data2<-arrange(my_data1$stateName, penumonia)
> arrange(my_data1$AK, pneumonia)

                                         hospital state     pneumonia
1                  PROVIDENCE ALASKA MEDICAL CENTER    AK          10.5
2  PEACEHEALTH KETCHIKAN MEDICAL             CENTER    AK          11.3
3                          SITKA COMMUNITY HOSPITAL    AK          11.5
4                        BARTLETT REGIONAL HOSPITAL    AK          11.6
5                    NORTON SOUND REGIONAL HOSPITAL    AK          11.6
6              PROVIDENCE KODIAK ISLAND MEDICAL CTR    AK          12.0
7                    MAT-SU REGIONAL MEDICAL CENTER    AK          12.1
8                          SOUTH PENINSULA HOSPITAL    AK          12.2
9                          ALASKA REGIONAL HOSPITAL    AK          12.5
10                      FAIRBANKS MEMORIAL HOSPITAL    AK          13.4
11               CENTRAL PENINSULA GENERAL HOSPITAL    AK          13.8
12                            MT EDGECUMBE HOSPITAL    AK          14.2
13                     ALASKA NATIVE MEDICAL CENTER    AK          15.5
14               YUKON KUSKOKWIM DELTA REG HOSPITAL    AK           9.7

显然是

14               YUKON KUSKOKWIM DELTA REG HOSPITAL    AK           9.7

应该在顶部。

将列类转换为数值后,结果相同。

所以我用

> class(my_data2$pneumonia)
[1] "character"
> my_data2$pneumonia <-as.numeric(my_data2$pneumonia)
Warning message:
NAs introduced by coercion 
> class(my_data2$pneumonia)
[1] "numeric

> arrange(my_data2,pneumonia)
                                           hospital state pneumonia
1                YUKON KUSKOKWIM DELTA REG HOSPITAL    AK       9.7
2                  PROVIDENCE ALASKA MEDICAL CENTER    AK      10.5
3  PEACEHEALTH KETCHIKAN MEDICAL             CENTER    AK      11.3
4                          SITKA COMMUNITY HOSPITAL    AK      11.5
5                        BARTLETT REGIONAL HOSPITAL    AK      11.6
6                    NORTON SOUND REGIONAL HOSPITAL    AK      11.6
7              PROVIDENCE KODIAK ISLAND MEDICAL CTR    AK      12.0
8                    MAT-SU REGIONAL MEDICAL CENTER    AK      12.1
9                          SOUTH PENINSULA HOSPITAL    AK      12.2
10                         ALASKA REGIONAL HOSPITAL    AK      12.5
11                      FAIRBANKS MEMORIAL HOSPITAL    AK      13.4
12               CENTRAL PENINSULA GENERAL HOSPITAL    AK      13.8
13                            MT EDGECUMBE HOSPITAL    AK      14.2
14                     ALASKA NATIVE MEDICAL CENTER    AK      15.5
15                 PROVIDENCE VALDEZ MEDICAL CENTER    AK        NA
16                       PROVIDENCE SEWARD HOSPITAL    AK        NA
17                 CORDOVA COMMUNITY MEDICAL CENTER    AK        NA

问题已经解决,谢谢评论

0 个答案:

没有答案