how to use lapply with mutate function
hello, I'm trying to use lapply with mutate function. I'm dealing with nested list data.
Let's take an example. given
is nested list with two elements. Each element is 10*2 list.
given<-replicate(2,list(matrix(unlist(replicate(10,sample(c(0.2,0.3,0.4,0.1),2,replace=FALSE),simplify=FALSE)),ncol=2)))
colnames(given[[1]])<-c('a','b')
colnames(given[[2]])<-c('a','b')
given
I will convert 0.1 and 0.2 to 'low', 0.3 to 'middle', 0.4 to 'high'. I used lapply, mutate and if_else function.
new_given<-lapply(seq_along(given), function(x){
mutate(x,
given[[x]][['new']] = if_else(given[[x]][['a']] %in% c(0.1,0.2),'low',
if_else(given[[x]][['I12']] %in% c(0.3),'middle','high')))})
However, the errored occured. It said there was an 'unexpected ')''. However, the number of bracket are paired right.
> new_given<-lapply(seq_along(given), function(x){
+ mutate(x,
+ given[[x]][['new']] = if_else(given[[x]][['a']] %in% c(0.1,0.2),'low',
Error: unexpected '=' in:
" mutate(x,
given[[x]][['new']] ="
> if_else(given[[x]][['I12']] %in% c(0.3),'middle','high')))})
Error: unexpected ')' in " if_else(given[[x]][['I12']] %in% c(0.3),'middle','high'))"
>
Would you tell me what was the problem and how to solve it?
*additional information : I read this article, Using lapply with mutate in R However, it used data.frame, not dealing with list data. So the approaches seemed different.