根据列内容=“私有”使用R创建一个新表

时间:2017-11-20 15:59:40

标签: r

我目前以名称schools存储在R中的下表,我正在寻找一种方法将表分成两个,具体取决于Status =“私有”还是“公共” ”

schools <- read.table(text = "Code  Status    Num   Letter
                             Cox        Private   133   a
                             Tend   Public    323   f
                             Folst  Private   234   i
                             Tam    Private   343   d
                             Escot  Public    433   q
                             Sandal Public    733   p
                             Salt   Public    37    m", header = TRUE)

我希望所有Status为“私立”的学校都进入学校私立,看起来像:

schoolsPrivate <- read.table(text = "Code    Status    Num   Letter
                             Cox     Private   133   a
                             Folst   Private   234   i
                             Tam     Private   343   d", header = TRUE)

1 个答案:

答案 0 :(得分:1)

您可以通过以下几种方式实现这一目标:

使用dplyr:

library(dplyr)

schoolsPrivate <- filter(schools, Status == "Private")

schoolsPublic <- filter(schools, Status == "Public")

您还可以使用拆分来分割df:

split_list <- split(schools, schools$Status)

哪个收益率:

$Private
   Code  Status Num Letter
1   Cox Private 133      a
3 Folst Private 234      i
4   Tam Private 343      d

$Public
    Code Status Num Letter
2   Tend Public 323      f
5  Escot Public 433      q
6 Sandal Public 733      p
7   Salt Public  37      m

并指定它:

schoolsPrivate <- split_list$Private