将一列中的某些模式与 R 中另一列中的 NA 交换

  • 本文关键字:一列 交换 NA 模式 r
  • 更新时间 :
  • 英文 :


在具有多列的数据中,我想将一列中的某些模式与另一列中的NA交换。

我有以下数据:

data = data.frame(adverb=c('truly','extremely','wanted','happily','stressed'),verb=c('loved','adored','a','prayed','the'),article=c('you','the','toy',NA,'importance'),argument=c(NA,'doll',NA,NA,NA))
adverb   verb    article argument
1     truly  loved        you     <NA>
2 extremely adored        the     doll
3    wanted      a        toy     <NA>
4   happily prayed       <NA>     <NA>
5  stressed    the importance     <NA>

我想根据下面的模式将数据中的值重新定位到相应的列。

adverb.pattern = '[a-z]+ly$'
verb.pattern = '[a-z]+ed$'
article.pattern = '(the)$|(a)$|(an)$'
argumen.pattern = '(you)$|(importance)$|(toy)$'

这是所需的输出。

adverb     verb article   argument
1     truly    loved    <NA>        you
2 extremely   adored     the       doll
3      <NA>   wanted       a        toy
4   happily   prayed    <NA>       <NA>
5      <NA> stressed     the importance

这是一个tidyverse解决方案:

# example data
data = data.frame(adverb=c('truly','extremely','wanted','happily','stressed'),
verb=c('loved','adored','a','prayed','the'),
article=c('you','the','toy',NA,'importance'),
argument=c(NA,'doll',NA,NA,NA),
stringsAsFactors = F)
library(tidyverse)
# specify patterns
adverb.pattern = '[a-z]+ly$'
verb.pattern = '[a-z]+ed$'
article.pattern = '(the)$|(a)$|(an)$'
argument.pattern = '(you)$|(importance)$|(toy)$'
data %>%
mutate(id = row_number()) %>%  # add row id (useful to reshape)
gather(type, value, -id) %>%   # reshape data
na.omit() %>%                  # remove rows with NAs
mutate(type_upd = case_when(grepl(adverb.pattern, value) ~ "adverb",       # check patterns sequentially
grepl(verb.pattern, value) ~ "verb",
grepl(article.pattern, value) ~ "article",
grepl(argument.pattern, value) ~ "argument"),
type_upd = ifelse(is.na(type_upd), type, type_upd)) %>%             # use original / initial type if updated type is NA
select(-type) %>%                         # remove old type
spread(type_upd, value) %>%               # reshape data
select(adverb, verb, article, argument)   # select column in this order
#      adverb     verb article   argument
# 1     truly    loved    <NA>        you
# 2 extremely   adored     the       doll
# 3      <NA>   wanted       a        toy
# 4   happily   prayed    <NA>       <NA>
# 5      <NA> stressed     the importance

最新更新