使用匹配字符串对数据帧中的行重新排序

Reorder rows in a dataframe with matching string

提问人:d.math 提问时间:10/5/2023 最后编辑:user16217248d.math 更新时间:10/6/2023 访问量:49

问:

我有一个数据帧,我想将所有具有“xy 转移诊断日期”的行推到列中具有“肝外转移位置”的行的正下方列中,列中列中具有“xy”的行。dfValue4Value2Value3

df <- data.frame(
  Measurement = c(
    "Were extra-hepatic metastases detected?", "Were extra-hepatic metastases detected?",
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
  ),
  Value = c(
    "yes", "no", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA
  ),
  Value2 = c(
    NA, NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA
  ),
  Value3 = c(
    NA, NA, "Peritoneum", NA, "Lymph_nodes", NA, "Lungs", NA, "Bones",
    NA, "Brain", NA, "Other", NA, "Other2", NA, "Other3", NA
  ),
  Value4 = c(
    NA, NA, NA, "Date of diagnosis of metastases in peritoneum",
    NA, "Date of diagnosis of metastases in lymph nodes", NA, "Date of diagnosis of metastases in lungs",
    NA, "Date of diagnosis of metastases in bones", NA, "Date of diagnosis of metastases in brain",
    NA, "Date of diagnosis of metastases in “other” location",
    NA, "Date of diagnosis of metastases in “other” location 2",
    NA, "Date of diagnosis of metastases in “other” location 3"
  ),
  Value5 = c(
    NA_character_, NA_character_, NA_character_, NA_character_,
    NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
    NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
    NA_character_, NA_character_, NA_character_, NA_character_
  )
)

我知道我可以通过以下方式做到这一点:

df_ordered <- df[c(1:3,11,4,12,5,13,6,14,7,15,8,16,9,17,10,18), ]

预期输出:

df_ordered <- data.frame(
  Measurement = c(
    "Were extra-hepatic metastases detected?", "Were extra-hepatic metastases detected?",
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
  ),
  Value = c(
    "yes", "no", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA
  ),
  Value2 = c(
    NA, NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA, "Location of extra-hepatic metastases", NA, "Location of extra-hepatic metastases",
    NA
  ),
  Value3 = c(
    NA, NA, "Peritoneum", NA, "Lymph_nodes", NA, "Lungs", NA, "Bones",
    NA, "Brain", NA, "Other", NA, "Other2", NA, "Other3", NA
  ),
  Value4 = c(
    NA, NA, NA, "Date of diagnosis of metastases in peritoneum",
    NA, "Date of diagnosis of metastases in lymph nodes", NA, "Date of diagnosis of metastases in lungs",
    NA, "Date of diagnosis of metastases in bones", NA, "Date of diagnosis of metastases in brain",
    NA, "Date of diagnosis of metastases in “other” location",
    NA, "Date of diagnosis of metastases in “other” location 2",
    NA, "Date of diagnosis of metastases in “other” location 3"
  ),
  Value5 = c(
    NA_character_, NA_character_, NA_character_, NA_character_,
    NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
    NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
    NA_character_, NA_character_, NA_character_, NA_character_
  )
)

有没有办法在不对行号进行硬编码并使用 tidyverse 与一些正则表达式结合使用的情况下做同样的事情?

r 正则表达式 dplyr tidyr

评论


答: 暂无答案