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.
CodePudding user response:
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