Обмен определенных шаблонов в одном столбце с NA в другом столбце в R - PullRequest
0 голосов
/ 08 октября 2018

В данных с несколькими столбцами я хочу обменять определенные шаблоны в одном столбце с 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

1 Ответ

0 голосов
/ 08 октября 2018

Вот решение 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...