A have a list that has the following structure.
mylist=list(y~ A,
y ~ A+B,
y ~ A+B+C)
I want to replace (recode) the “y “
with a “z”
. my goal is
mylist=list(z~ A,
z ~ A+B,
z ~ A+B+C)
Q: How to replace (recode) values in a list?
I have tried this:
for i in range(len(mylist)):
mylist[i] = mylist[i].replace('y','z')
is not working
The update
function is useful for formulas.
Just include a .
to indicate any formula side to retain. So, for your problem the following is a quick one-liner.
lapply(mylist, update, new = z~.)
I would alternatively suggest to use R built in formulas manipulation functionality. This allows us to operate on different terms of a fromula separately without using regex
lapply(mylist, function(x) reformulate(as.character(terms(x))[3], "z"))
# [[1]]
# z ~ A
# <environment: 0x59c6040>
#
# [[2]]
# z ~ A + B
# <environment: 0x59c0308>
#
# [[3]]
# z ~ A + B + C
# <environment: 0x59bb7b8>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With