I have a dataset, which looks like this
Col1 Col2 Col3
A B NA
AA C NA
D CC NA
E F NA
I would like to add data to col3 based on condition. If col1 and col2 only have one letter each, write "SNP". If col1 has more than one letter, write "DEL" and if col2 has more than one letter, write "INS"
Final product would be:
Col1 Col2 Col3
A B SNP
AA C DEL
D CC INS
E F SNP
Anyone would know how to do this in R?
Thank you!
You can use two nested ifelse
statements. So, for example, using dplyr::mutate
:
library(dplyr)
df = df %>% mutate(Col3 = ifelse(nchar(Col1)>1,"DEL",ifelse(nchar(Col2)>1,"INS","SNP")))
Col1 Col2 Col3
1 A B SNP
2 AA C DEL
3 D CC INS
4 E F SNP
A tidyverse solution:
library(magrittr); library(dplyr); library(stringr)
df %>% mutate(Col3 = case_when(str_length(Col1) == 1 & str_length(Col2) == 1 ~ "SNP",
str_length(Col1) > 1 ~ "DEL",
str_length(Col2) > 1 ~ "INS")
)
Col1 Col2 Col3
1 A B SNP
2 AA C DEL
3 D CC INS
4 E F SNP
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.