Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining mutate and filter functions

I am a beginner when it comes to R language so sorry if I am duplicating a question btw I use tidyverse packages.

My problem is at follows: I have a dataframe in which one column looks like that

pre_schwa
IY0
SH
Z
+1500 rows

Now I need to create a column(variable) which corresposnds to this specific column. I created four vectors:

vowels <- c("AY1", "ER0", "IY0", "IY1", "UW2")
sonorants <- c("M","N", "R", "Y", "ZH", "W")
fricatives <- c("F", "S", "SH", "TH", "V", "Z")
stops <- c("B", "CH", "D", "G", "JH", "K", "P", "T")

Having this I want to create a column called sonority_grouped which would consist of four names(vowels, sonorants, fricatives, stops) depending what character is in the pre_schwa column so I want it to look like this

  pre_schwa              sonority_grouped
  SH                     fricatives
  ER0                    vowels
  B                      stops
  Z                      fricative
  +1500 rows

I tried combining mutate() and filter() functions by %>% but I suck at programming. Thank you for any reponse.

like image 904
phnlg Avatar asked Dec 23 '22 07:12

phnlg


2 Answers

You can also use case_when.

df %>%
  mutate(sonority_grouped = case_when(
    pre_schwa %in% vowels ~ "vowels",
    pre_schwa %in% sonorants ~ "sonorants",
    pre_schwa %in% fricatives ~ "fricatives",
    pre_schwa %in% stops ~ "stops",
  ))
like image 179
hpesoj626 Avatar answered Dec 28 '22 09:12

hpesoj626


Data

df <- read.table(text="pre_schwa
IY0
SH
Z", header=TRUE, stringsAsFactors=FALSE)

I recommend converting your individual vectors into a data.frame via

vowels <- c("AY1", "ER0", "IY0", "IY1", "UW2")
sonorants <- c("M", "N",  "R",  "Y",  "ZH", "W")
fricatives <- c("F", "S", "SH", "TH", "V", "Z")
stops <- c("B", "CH", "D", "G", "JH", "K", "P", "T")

patterns <- c("vowels", "sonorants", "fricatives", "stops")
df2 <- stack(mget(patterns))

Alternatively, as pointed by MrFlick, you can use lattice::make.groups(...)

df2 <- lattice::make.groups(vowels, sonorants, fricatives, stops) %>% 
          dplyr::rename(pre_schwa=data, sonority_grouped=which)    

Then you can use dplyr::left_join to obtain your result

ans <- dplyr::left_join(df, df2, by=c("pre_schwa" = "values"))
  # pre_schwa        ind
# 1       IY0     vowels
# 2        SH fricatives
# 3         Z fricatives

With MrFlick's answer use

ans <- dplyr::left_join(df, df2)
like image 32
CPak Avatar answered Dec 28 '22 09:12

CPak