Is there a way to check if values in column has spaces in it?
asd <- data.frame(a = c("new ", "Old"), b = c("Cta", "df"))
We have space in "a" column. So expected output to be
a b spaceInCola
1 new Cta TRUE
2 Old df FALSE
CodePudding user response:
Sure. Here's an easy way with grepl
:
asd <- data.frame(a = c("new ", "Old"), b = c("Cta", "df"))
asd$spaceInCola <- grepl(' ', asd$a)
asd
#> a b spaceInCola
#> 1 new Cta TRUE
#> 2 Old df FALSE
CodePudding user response:
A tidyverse
approach:
library(dplyr)
library(stringr)
asd %>%
mutate(spaceInCola = str_detect(a, "\\s"))
# a b spaceInCola
#1 new Cta TRUE
#2 Old df FALSE
Useful to use dplyr
in that case if you want to check over multiple columns:
asd %>%
mutate(across(a:b, ~ str_detect(.x, "\\s"),
.names = "SpaceInCol{col}"))
# a b SpaceInCola SpaceInColb
# 1 new Cta TRUE FALSE
# 2 Old df FALSE FALSE
CodePudding user response:
Option using sapply
:
asd$spaceInCola <- sapply(asd$a, \(x) grepl(' ', x))
asd
#> a b spaceInCola
#> 1 new Cta TRUE
#> 2 Old df FALSE
Created on 2022-10-20 with reprex v2.0.2
Option using sapply
with the sum of TRUE's. This is for row-wise checking:
asd$spaceInCola <- sapply(asd, \(x) as.logical(sum(grepl(' ', x))))
asd
#> a b spaceInCola
#> 1 new Cta TRUE
#> 2 Old df FALSE
Created on 2022-10-20 with reprex v2.0.2
CodePudding user response:
Using stringr
:
asd$spaceInColA <- str_detect(asd[,1], " ")
Or with stringi
:
asd$spaceInColA <- stri_detect_fixed(asd[,1]," ")
Outupt is the same for both functions:
> asd
a b spaceInColA
1 new Cta TRUE
2 Old df FALSE