Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a condition that if a value (that is a character) starts with a specific word, then I need to print a vector?

I have this dataframe that I applied ifelse function and startsWith function

df = data.frame(x = c('part1',letters[1:3],'part2',letters[5:6]))

y = 5:6

df$y = ifelse(startsWith(df$x,'part'),y,NA)
df

However the output is

      x  y
1 part1  5
2     a NA
3     b NA
4     c NA
5 part2  5
6     e NA
7     f NA

The column y includes 5 and 5 instead of 5 and 6. I don’t know what the error is I am making. Thanks for the help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

ifelse probably isn’t the right tool here.

If you want a one-liner solution in base R you could use replace:

within(df, y <- replace(rep(NA, nrow(df)), startsWith(df$x, 'part'), y))
#>       x  y
#> 1 part1  5
#> 2     a NA
#> 3     b NA
#> 4     c NA
#> 5 part2  6
#> 6     e NA
#> 7     f NA

Although I guess most folks would probably just create an NA column and write y into the matching indices.

df$y <- NA
df$y[startsWith(df$x, 'part')] <- y

df
#>       x  y
#> 1 part1  5
#> 2     a NA
#> 3     b NA
#> 4     c NA
#> 5 part2  6
#> 6     e NA
#> 7     f NA

Both approaches of course assume that the number of matches is the same as the length of y, and you probably need some extra logic to check that this is definitely the case.

Created on 2022-11-10 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading