Plot data from lists in R

here is the problem. I have two lists of vectors sorted (hopefully, but I can sort them if they arent), where the i-th vector in the first list has as many numbers as the i-th vector in the second list. I just want to capture them. But I'm afraid that list items will not be displayed. Any ideas how to fix this? Thank you very much. Here is the code I tried.

a<-c(2,1,5) b<-c(2,2,2) f<-c(4) g<-c(1) k<-list(a,f) l<-list(b,g) for(i in 1:2){ plot(l[i],k[i])} 

and the problem

 Error in xy.coords(x, y, xlabel, ylabel, log) : (list) object cannot be coerced to type 'double' 
+5
source share
3 answers

The best way to do this is to avoid for-loop and write down lists to build them.

This is the way to use unlist :

 plot(unlist(l),unlist(k)) 

enter image description here

The way to do this with for-loop would be as follows:

 for (i in 1:2) { par(new=T) plot(l[[i]], k[[i]], xlim=c(0,2), ylim=c(0,5)) } 

But this is completely optional, since you can get the same result simply by blocking it. You should also use par(new=T) so that the second (or any other) graph does not overwrite the previous ones, and you would need to specify the x and y limits so that the two graphs have the same scale. In addition, you will need to use double square brackets [[]] , as @HubertL mentions in his answer to accessing lists. The result will be the same as above (with labels in a bolder format, since the labels will be displayed twice on top of each other).

+4
source

You can use double brackets [[]]:

 plot(l[[i]],k[[i]]) 
+1
source

Almost there, like @HubertL, just two brackets

 a<-c(2,1,5) b<-c(2,2,2) f<-c(4) g<-c(1) k<-list(a,f) l<-list(b,g) for(i in 1:2){ plot(l[[i]],k[[i]]) } 

First enter image description here

0
source

Source: https://habr.com/ru/post/1232653/


All Articles