data <- data.frame(
"grammer" = c("Python","C","Java","GO",NA,"SQL"),
"score" = c(1,2,NA,4,7,10),
"file" = c("GHG.txt", "GXG.ect", NA , "VAC.ect", "GBA.ect", "GHG.txt"),
"file2" = c("GHG.txt", "GXG.dat", "AGG.ect", "VAC.txt", "GBA.dat", "GHG.dat"),
"file3" = c("GHG.dat", "GXG.txt", "AGG.dat", "VAC.dat", "GBA.txt", NA )
)
I want to get suffix .ect from columns (file, file2, file3) and mutate in new column.
Following is my code. I want change to filter simultaneously.
d1 <- data %>%
filter(str_detect(file, ".ect")) %>%
mutate(sub("\\..*", "", file))
d2 <- data %>%
filter(str_detect(file2, ".ect")) %>%
mutate(sub("\\..*", "", file2))
d3 <- data %>%
filter(str_detect(file3, ".ect")) %>%
mutate(sub("\\..*", "", file3))
Here is my expected outcome :
