I am facing a problem when trying to subset my data, maybe you could help me. What I need is to subset data from first data frame by a column when this column value is equal to the value of a column in the second data frame.
The following are the dataframes I'm using:
> head(places)
Zona Poble lat lon alt
1 1 Zorita 40.7353 -0.165748 691.867
2 1 Morella 40.6287 -0.113284 955.719
3 1 Forcall 40.6621 -0.209759 753.882
4 2 Benasal 40.3943 -0.126111 848.171
5 2 Cati 40.4532 0.060409 667.610
6 2 Fredes 40.7079 0.167981 1194.730
> head(data)
date time stat_id lat lon tempc
1 20121122 000000 1 40.7353 -0.1657 7.98737
2 20121122 000000 2 40.6287 -0.1133 6.49903
3 20121122 000000 3 40.6621 -0.2098 7.72955
4 20121122 000000 4 40.3943 -0.1261 7.98837
5 20121122 000000 5 40.4532 0.0604 10.35480
6 20121122 000000 6 40.7079 0.1680 6.00769
As you can see, three first places in dataframe "places" belong to Zona == 1 and share lat/lon with three first rows in dataframe "data". I would like to select rows in data that share lat/lon with Zona == i on places.dat.
The R script I am trying is
datos=read.table("data.dat",header=T)
places=read.table("places.dat",header=T)
data=as.data.frame(datos)
place=as.data.frame(pobles)
data$time[data$time == 0] = "000000"
subset(data,data$lat == place$lat[place$Zona == 1])
So, subset would show three rows for each time in data.dat but it is only selecting two of three, as it follows
> subset(data,data$lat == place$lat[place$Zona == 1])
date time stat_id lat lon tempc
1 20121122 000000 1 40.7353 -0.1657 7.98737
2 20121122 000000 2 40.6287 -0.1133 6.49903
385 20121122 30000 1 40.7353 -0.1657 7.00632
386 20121122 30000 2 40.6287 -0.1133 4.83684
769 20121122 60000 1 40.7353 -0.1657 6.55283
770 20121122 60000 2 40.6287 -0.1133 4.85467
1153 20121122 90000 1 40.7353 -0.1657 6.35216
1154 20121122 90000 2 40.6287 -0.1133 5.66342
1537 20121122 120000 1 40.7353 -0.1657 11.47750
1538 20121122 120000 2 40.6287 -0.1133 10.30310
1921 20121122 150000 1 40.7353 -0.1657 13.87090
1922 20121122 150000 2 40.6287 -0.1133 11.90640
2305 20121122 180000 1 40.7353 -0.1657 10.30840
2306 20121122 180000 2 40.6287 -0.1133 7.61322
2689 20121122 210000 1 40.7353 -0.1657 6.29745
2690 20121122 210000 2 40.6287 -0.1133 6.63173
3073 20121123 000000 1 40.7353 -0.1657 4.78633
3074 20121123 000000 2 40.6287 -0.1133 5.31070
3457 20121123 30000 1 40.7353 -0.1657 6.84001
3458 20121123 30000 2 40.6287 -0.1133 6.88369
3841 20121123 60000 1 40.7353 -0.1657 5.71790
For sure I'm missing something, could you help me? Any idea or hint will be appreciated.
Thanks
Data files are available here:
- data.dat http://ubuntuone.com/7XLDg7woFQKjqiRRJeiuQs
- places.dat http://ubuntuone.com/2mO8TrAbGBzr5jTTLWlxaq
EDIT Following answer from @A.R I tried this code to select data but not sure if it is just the exact way.
for(i in 1:128) {
for(j in 1:2) {
a=sqrt((place$lat[i]-datos$lat[j])^2+(place$lon[i]-datos$lon[j])^2)
n=which.min(a)
while(n <= 9344) {
b=cbind(i,n,datos$tempc[n],place$Zona[i])
n=n+128
}
}
}
and get:
> b
i n
[1,] 128 9217 10.1198 30
it gives just the value for the last i value, I would like to save all. Sure it is a basic but I can't figure out, please be patient as I'm not a experienced R user. Thanks again