Home > Software engineering >  how to use a function on dataframe
how to use a function on dataframe

Time:10-06

I am trying to use substr to filter a dataframe, for example, filter the dataframe to rows that 3rd column strats with "I". trying which(substr(df[,3],1,1)=="I"), an it doesn't work. taking a variable and checking substr(var,1,1)=="I", works.

CodePudding user response:

I cannot replicate your issue. That is why reproducible data is important. Use dput(head(df)) to provide a sample of your data.

set.seed(42)
var1 <- sample.int(100, 10)
var2 <- paste0(sample(c("|", ""), 10, replace=TRUE), sample(LETTERS, 10))
dat <- data.frame(var1, var2)
dat
#    var1 var2
#  1   49   |D
#  2   65    E
#  3   25    M
#  4   74    Y
#  5   18    T
#  6  100   |B
#  7   47   |H
#  8   24   |C
#  9   71   |A
# 10   89   |J
which(substr(dat[, 2], 1, 1) == "|")
# [1]  1  6  7  8  9 10
which(substr(var2, 1, 1) == "|")
# [1]  1  6  7  8  9 10
which(grepl("^\\|", dat[, 2]))
# [1]  1  6  7  8  9 10

The last is an alternate approach using grepl().

CodePudding user response:

An option with str_detect

library(dplyr)
library(stringr)
df1 %>%
    filter(str_detect(col3, fixed("|")))
  •  Tags:  
  • r
  • Related