【问题标题】:R: Subsetting df based on another column entryR:子集 df 基于另一个列条目
【发布时间】:2017-03-15 22:18:15
【问题描述】:

我假设我正在处理一个简单的问题,即基于另一列值(此处为患者)对数据进行子集化,但找不到解决方案。我需要为每位住院至少 4 次的患者提供一个数据子集。换句话说,只有在医院至少住过 4 次的患者才会在新的 df 中显示他们的 4 次就诊行。我的桌子是这样的:

</style>
<table class="tg">
  <tr>
    <th class="tg-yw4l">Patient</th>
    <th class="tg-yw4l"># Hospital Visits</th>
    <th class="tg-yw4l">Duration</th>
  </tr>
  <tr>
    <td class="tg-yw4l">Monica</td>
    <td class="tg-yw4l">1</td>
    <td class="tg-yw4l">10D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Jack</td>
    <td class="tg-yw4l">1</td>
    <td class="tg-yw4l">5D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Monica</td>
    <td class="tg-yw4l">2</td>
    <td class="tg-yw4l">3D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Eric</td>
    <td class="tg-yw4l">1</td>
    <td class="tg-yw4l">2D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Eric</td>
    <td class="tg-yw4l">2</td>
    <td class="tg-yw4l">3D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Monica</td>
    <td class="tg-yw4l">3</td>
    <td class="tg-yw4l">4D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Jack</td>
    <td class="tg-yw4l">2</td>
    <td class="tg-yw4l">4D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Eric</td>
    <td class="tg-yw4l">3</td>
    <td class="tg-yw4l">8D</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Eric</td>
    <td class="tg-yw4l">4</td>
    <td class="tg-yw4l">9D</td>
  </tr>
</table>

非常感谢!

【问题讨论】:

  • 你的表格是html代码?
  • 等等,你的数据是 HTML 表格吗?或者您在 R 中是否有适当的 data.frame。请参阅how to create a reproducible example in R 了解在问题中包含数据的方法(不要将 sn-ps 用于 R 代码或数据——那些用于 HTML/javascript)

标签: r subset


【解决方案1】:
df1 <- readHTMLTable(doc)[[1]]
colnames( df1 ) <- gsub("# ", '', colnames( df1 ))
df1$`Hospital Visits` <- as.numeric( df1$`Hospital Visits`)

df1
#   Patient Hospital Visits Duration
# 1  Monica               1      10D
# 2    Jack               1       5D
# 3  Monica               2       3D
# 4    Eric               1       2D
# 5    Eric               2       3D
# 6  Monica               3       4D
# 7    Jack               2       4D
# 8    Eric               3       8D
# 9    Eric               4       9D

仅获取患者访问医院至少 4 次的事件

with( df1, df1[ `Hospital Visits` >= 4, ] )
#   Patient  Hospital Visits Duration
# 9    Eric                4       9D

获取患者访问医院至少 4 次的所有事件

do.call( 'rbind', lapply( split( df1, df1$Patient ), 
                          function( x ) if( any(x$'Hospital Visits' >= 4 ) ) { x }) )

#        Patient Hospital Visits Duration
# Eric.4    Eric               1       2D
# Eric.5    Eric               2       3D
# Eric.8    Eric               3       8D
# Eric.9    Eric               4       9D

数据:

library(XML)
doc <- htmlParse('<table class="tg">
                 <tr>
                 <th class="tg-yw4l">Patient</th>
                 <th class="tg-yw4l"># Hospital Visits</th>
                 <th class="tg-yw4l">Duration</th>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Monica</td>
                 <td class="tg-yw4l">1</td>
                 <td class="tg-yw4l">10D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Jack</td>
                 <td class="tg-yw4l">1</td>
                 <td class="tg-yw4l">5D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Monica</td>
                 <td class="tg-yw4l">2</td>
                 <td class="tg-yw4l">3D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Eric</td>
                 <td class="tg-yw4l">1</td>
                 <td class="tg-yw4l">2D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Eric</td>
                 <td class="tg-yw4l">2</td>
                 <td class="tg-yw4l">3D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Monica</td>
                 <td class="tg-yw4l">3</td>
                 <td class="tg-yw4l">4D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Jack</td>
                 <td class="tg-yw4l">2</td>
                 <td class="tg-yw4l">4D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Eric</td>
                 <td class="tg-yw4l">3</td>
                 <td class="tg-yw4l">8D</td>
                 </tr>
                 <tr>
                 <td class="tg-yw4l">Eric</td>
                 <td class="tg-yw4l">4</td>
                 <td class="tg-yw4l">9D</td>
                 </tr>
                 </table>')

【讨论】:

    【解决方案2】:

    有无数种方法可以做到这一点,一种简单的方法,虽然不是最有效的......

    假设您在数据框中有这个,您可以过滤掉具有 4 个或更多的 ID(在本例中为名称)。然后显示这些名称的所有记录。我将您的原始数据框命名为 my_df

    who_to_include <- subset(unique(my_df$name),hospital_visits>=4)
    library(dplyr)
    4_or_more <- inner_join(who_to_include,my_df)
    

    抱歉,这里没有示例,所以我只是在这里修改这段代码,可能不是 100% 正确,或者它可能是

    【讨论】:

      【解决方案3】:

      假设您在数据框中有此内容,并且“患者”列的内容唯一地指定了一位患者(即没有多个 Eric),您也可以仅使用基本 R 对其进行子集化:

      # Find row numbers of entries with number of visits >= 4
      frequentPatientRows <- patientsDf[, "# Hospital Visits"] >= 4
      # Extract names from those rows
      frequentPatientNames <- patientsDf[frequentPatientRows, "Name"]
      # Select all entries for patients with those names
      selectedPatients <- patientsDf[patientsDf[, "Name"] %in% frequentPatientNames, ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-29
        • 2022-12-05
        • 1970-01-01
        • 2020-10-24
        • 2021-06-22
        • 2020-06-26
        • 2016-03-12
        相关资源
        最近更新 更多