How to assign something to rows of df with selected columns

Hi,
I have a question because I am stuck for a while.

res[2,3:9]  %>%   str_replace_all("^1", "2" )

this works, but how to assign it, I mean how to save it ?

I have tried:

res[2,3:9]  %>%   str_replace_all("^1", "2" ) -> res[2,3:9]

but it does not work. I would like to change value 1 to value 2 in row 2 of df named res, in selected columns 3 to 9.

This is relatively simple, so I reverse engineered res, but see the FAQ: How to do a minimal reproducible example reprex for beginners for what to do to post questions that don't require community contributors to do reverse engineering.

res <- rep("1",9)
res[2:9] <- gsub("1","2",res[2:9])
res
#> [1] "1" "2" "2" "2" "2" "2" "2" "2" "2"

Created on 2022-11-16 by the reprex package (v2.0.1)

Can you provide a reprex? it should work as long as columns 3:9 are of class "character" because that is the output class of str_replace_all()

Thank you both,
what worked for me finally was adding as.list() to the pipe:

res[2,3:9]  %>%   str_replace_all("^1", "2" ) %>% as.list() -> res[2,3:9]

Of course next time I will prepare a Reprex.
Thanks again.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.