Week5: pattern matching in dataframe

R-intermediate
Author

Tien-Cheng

Welcome to the fifth course! You will learn more about dataframe wrangling:

Learning goals
  1. data frame wrangling with dplyr and tidyr

1 Today’s discussion

Is it possible to access the elements at different columns and rows?

df <- data.frame(
  x1=1:3,
  x2=letters[1:3],
  x3=c("2a","2b","2c")
)
# or condition separate by |
df$x1==2|df$x3=="2c"

df %>% filter(x1==2|x3=="2c")
df %>% with(.,x1==2|x3=="2c")

# when not specifying the comma, it will be treated like column
df %>% with(.,.[x1==2|x3=="2c"])
# specify the rows
df %>% with(.,.[x1==2|x3=="2c",])

2 more mutate examples

Last week’s practice.

df <- expand.grid(x=letters[1:4],
                  y=1:2)%>%
  # combine columns x and y 
  mutate(z=interaction(x,y))
rownames(df) <- LETTERS[1:nrow(df)]

2.1 replace column

2.1.1 replace one column based on single condition

df %>% mutate(k=ifelse(x=="a","A","B"))
df %>% mutate(k=ifelse(y==1,"A","B"))
df %>% mutate(k=case_when(x=="a"~"A",
                          TRUE~"B"))
practice

Matching multiple conditions

add column k to df, when the condition x equals “a” and y equals 1. if TRUE return ‘A’, else return ‘B’.

2.1.2 replace one column based on multiple conditions

in case_when syntax, TRUE before ~ stands for the else conditions.

df %>% mutate(k=case_when(x=="a"~"A",
                          x=="b"~"B",
                          TRUE~"C"))

2.1.3 Look up table

look_table <- data.frame(x=letters,
                         X=LETTERS)
df %>% merge(look_table)
  x y   z X
1 a 1 a.1 A
2 a 2 a.2 A
3 b 1 b.1 B
4 b 2 b.2 B
5 c 1 c.1 C
6 c 2 c.2 C
7 d 1 d.1 D
8 d 2 d.2 D
practice

merge is not actually replace the original column.

Write a function to replace letters with LETTERS. The input is a vector of lower case vector vec <- c("c","a","b","d"), output will be the matched upper case vector c("C","A","B","D").

Hint: check function match() !!Before you start to write the code, please first write down the possible steps in text.!!