A user received an error while performing simple calculation in a list of lists.

695    Asked by Rohit sharma in Data Science , Asked on Jan 6, 2020
Answered by Rohit sharma

l1 <- list(a=list(1:4),b=list(1:6))

l2 <- list(a=list(1:4),b=list(1:6))

After performing l1+l2, the error is following

non-numeric argument to binary operator

To solve such errors, we can use lapply() function to go through each list simultaneously.

lapply(seq_along(l1),function(i)

unlist(l1[i])+unlist(l2[i]))

[[1]] a1 a2 a3 a4

       2 4 6 8

[[2]] b1 b2 b3 b4 b5 b6

      2 4 6 8 10 12



Your Answer

Interviews

Parent Categories