Reputation: 1378
I have a lot of coordinates:
> xh1
[1] 257283.7 258592.6 261811.6 262768.6 257283.7
> yh1
[1] 2639722 2640722 2640722 2639722 2639722
> xh2
[1] 257283.7 256523.3 263725.6 262768.6 257283.7
> yh2
[1] 2639722 2638722 2638722 2639722 2639722
... ...
Using 'cbind', I want to prepare the coordinates to be converted into polygons as:
poly1<-cbind(xh1,yh1)
poly2<-cbind(xh2,yh2)
... ...
> poly1
xh1 yh1
[1,] 257283.7 2639722
[2,] 258592.6 2640722
[3,] 261811.6 2640722
[4,] 262768.6 2639722
[5,] 257283.7 2639722
... ...
poly<- Polygon(poly1)
... ...
Is it possible to use a loop to do the 'cbind' task in R?? I know anything like below is not going to work:
poly<-lapply(1:100, function(i) cbind(paste0("xh",i), paste0("yh",i)))
Upvotes: 1
Views: 379
Reputation: 801
Use get
to fetch the value of a variable given its name. So in your code above, replace paste0("xh",i)
with get(paste0("xh",i))
, and likewise for yh
. In other words, try
poly<-lapply(1:100, function(i) cbind(get(paste0("xh",i)), get(paste0("yh",i))))
(I can't help wondering why you're using separate variables xh1
, xh2
, ... instead of a matrix or data frame xh
with columns 1, 2, ...)
Upvotes: 2