1

I have a list lst1 and a dataframe from another list lst2[[df]]. If I want to append df to lst1 with the condition !is.null(lst2[[df]]), what should I do? I would like to know the way if I use lapply as well as append

For append, I think we can do append(lst1, list(lst2[[df]]). I am not sure where to add condition. I also have the doubt on list(lst2[[df]]) part. Could anyone guide me on this?

Thanks.

Stataq
  • 2,237
  • 6
  • 14
  • 1
    Please provide a reproducible example. – s_baldur Aug 12 '22 at 15:24
  • It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 12 '22 at 15:29

1 Answers1

2

We can use the condition in if (assuming df is an object for the list name)

if(!is.null(lst2[[df]])) lst1[[df]] <- lst2[[df]]
akrun
  • 874,273
  • 37
  • 540
  • 662