Use of gsub and sub function in a list

mylist <- list(a=c("x","y","z"),b=c(1,2,3))

How do I replace x with another letter for example "g" ?

I got a similar question from exercise no. 4.5 in "The book of R " by Tilman M daviies.
I was using this to learn R and could not find a solution to the question.

Yes it does.

Can you explain how it was done?

lapply applies a function over each element of a list. So, here the anonymous function passed to FUN is applied to both a and b of mylist.

Since you specifically asked for gsub, I guess you already know what it does. I used the extra fixed argument as I was searching for literal match instead of a regular expression.

apply family of functions are very useful. I think it is worth reading the documentation and experimenting with these yourself.

1 Like

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