1

I am drawing spatial samples from a shp file. In this way:

pr <- readShapePoly("PuertoRico.shp")
 n <- 90
 samp.reg <- spsample(pr, n, type="regular")

This is what I get:

samp.reg
SpatialPoints:
         x1       x2
 #[1,] -66.15482 17.93279
 #[2,] -67.15842 18.02403
 #[3,] -67.06718 18.02403
 #[4,] -66.97595 18.02403
 #[5,] -66.88471 18.02403

And so on, till [90,]

Now, I would like to extract those observations included in the sample just drawn , from the dataframe containing not only the coordinates, but also the connected variable that I need to study (i.e. elevation). Below you can see how the dataframe looks like.

df <- read.csv("pr_elev_rs.csv",header=TRUE)
 head(df)
  Label         x        y elevation
1 BL192 -67.27045 18.36269         0
2 BM191 -67.26450 18.36761        30
3 BM192 -67.26490 18.36020        56
4 BM193 -67.26287 18.35222         0
5 BM194 -67.26075 18.34632         0
6 BN190 -67.25191 18.37403        10

I am a beginner with R and I don't know how I can do this. Thank you for the help, I really need it.

chrki
  • 6,143
  • 6
  • 35
  • 55
27titanik
  • 21
  • 5

1 Answers1

0

Not sure I have undestood correctly your question.

 match <- df$x %in% samp.reg@coords[,1] & df$y %in% samp.reg@coords[,2]
 extract_df_row <- df[match,]

Is this what you need??

EDIT: now you can subset

 if(nrow(extract_df_row)>0) {
  coordinates(extract_df_row) <- cbind(extract_df_row$x, extract_df_row$y)
  }else{
    cat("no coordinates matching df")
  }
G. Cocca
  • 2,456
  • 1
  • 12
  • 13
  • Now it works! Thanks! However, it says that there are no rows that match the coordinates and this can't be possible. – 27titanik Apr 06 '16 at 13:35
  • mmm.. really strange. I substituted the first coordinates of df with the first coordinates of samp.reg in order to create a match since the piece of data provided doesn't show any match (the if ... else statement was introduced for that) and it works for me.. Are you sure there is a match in your data?? – G. Cocca Apr 06 '16 at 14:27
  • Maybe there are some issues related to the coordinates. To check if there are any match, I just wanted to take a random x coordinate from one set and try to find the rows with the same coordinate in the other. But, when I just try to find how many rows there are with this coordinate, x1 <- df[df$x==-65.66646,] (for instance), dim(x1) says that there are no ones. But I can see it in the data set when I tip (df) – 27titanik Apr 06 '16 at 18:13
  • try checking that the class of your column's coordinates `class(df$x)`. It sounds like your column was coerced to a character or to a factor. But it's hard to say without the complete dataset. – G. Cocca Apr 06 '16 at 20:37
  • class(df$x) " numeric" when I search for a row with a given coord, it says this :" <0 rows> (or 0-length row.names)" – 27titanik Apr 07 '16 at 05:53
  • If you can share an exemple (e.g. the piece of dataset where the problem takes place) I can try to help, otherwise this trial and error could take days :). Consider to edit this thread or eventually post a new question. – G. Cocca Apr 07 '16 at 06:55
  • 9093 IZ209 -65.66790 18.20855 0 this is the last column that i can see, by tipping " df". As you can see, the third one is df$x, the fourth df$y, and the fitfh is the value of the variable that I am studying. I have tried to extract from it all the rows with the coordinate - 65.66790, in this way: x1 <- df$x=="-65.66790" , but if then I do this : dim(x1) it gives "NULL". I tried it out to check what is wrong with this dataset, since the matching function (that you wrote) didn't give any result. – 27titanik Apr 07 '16 at 07:03
  • you should be able to subset it with df[df$x==-65.66790,]. If not consider to remove everything from environment typing `rm(list=ls())` restart your R session and reimport everything. I can't do more than this... – G. Cocca Apr 07 '16 at 07:14
  • I met my professor and we that there are some problems with the dataset ! Anyway, thank you for your help! – 27titanik Apr 10 '16 at 07:47