I want to find out if cells in a data frame column contain a specific character in R.
I created a column called df1$var_sting
using the code below:
df1 <- data.frame(
var_sting =
c(
"test1",
"test2 + test3"
)
)
I want to create a column called df1$plus_test_short
, which contains a "yes/no"
answer to the question on whether a character ("+") is present in the variable called "df1$var_sting".
I can do this the long way, using the code below:
df1$plus_test_long <-
c(
"no",
"yes"
)
When I try to do this the short way, using the grepl()
command, it does not give the desired result:
> df1$plus_test_short
[1] "yes" "yes"
Is there any way to do this using a short way, that does not require me to input results individually in the plus_test_
prefixed columns that correspond to the appropriate rows in the df1$var_sting
variable?
Please advise. Thanks a head of time.
# stackoverflow example data
## creates object
df1 <- data.frame(
var_sting =
c(
"test1",
"test2 + test3"
)
)
### displays object
df1
## desired new column, which tells if there is a + (plus sign) in the cell
### done the long way, which requires manual input
df1$plus_test_long <-
c(
"no",
"yes"
)
## done the short way, using grepl() command, which does not require manual entering of "yes" and "no" with each cell in corresponding column
df1$plus_test_short <-
ifelse(((grepl("+", df1$var_sting)) == TRUE), "yes", "no")
df1$plus_test_short
# ---- NOTE: does not work