Home > other >  how do we remove values inside the square brackets in R code
how do we remove values inside the square brackets in R code

Time:03-09

how do we remove values inside the square brackets in R-code for an entire dataframe for each column - ex:string "hello[1,2,3]"

CodePudding user response:

You can use sub:

string <- "hello[1,2,3]"
sub("\\[.*\\]", "", string)
#> [1] "hello"

For an entire data frame like this:

set.seed(1)

df <- as.data.frame(matrix(paste0(sample(c("hello", "goodbye"), 10, TRUE), "[",
        sample(10), ",", sample(10), ",", sample(10), "]"), ncol = 2))

df
#>                V1              V2
#> 1    hello[3,5,1]    hello[6,7,7]
#> 2  goodbye[1,9,4]   hello[10,2,5]
#> 3    hello[5,1,3]    hello[9,4,8]
#> 4    hello[8,6,6]  goodbye[4,3,9]
#> 5 goodbye[2,10,2] goodbye[7,8,10]

You can do

df[] <- lapply(df, function(x)  sub("\\[.*\\]", "", x))

df
#>        V1      V2
#> 1   hello   hello
#> 2 goodbye   hello
#> 3   hello   hello
#> 4   hello goodbye
#> 5 goodbye goodbye

Created on 2022-03-08 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related