Home > database >  R remove all characters after a slash in a dataframe
R remove all characters after a slash in a dataframe

Time:07-19

I have some rows that need to have everything after a slash removed

x1\
myco\
myco\
myco/fungicide

is there a way I can do this?

CodePudding user response:

We may use trimws

df1$x1 <- trimws(df1$x1, whitespace = "/.*")

CodePudding user response:

Another option using gsub:

df <- read.table(text = "v1
x1\
myco\
myco\
myco/fungicide", header = TRUE)
df
#>               v1
#> 1             x1
#> 2           myco
#> 3           myco
#> 4 myco/fungicide
gsub("/.*","",df$v1)
#> [1] "x1"   "myco" "myco" "myco"

Created on 2022-07-19 by the reprex package (v2.0.1)

  • Related