Skip to content

Local_coord

4 messages · Kátia Emidio, Loïc Dutrieux, Oscar Perpiñan +1 more

#
Hi there,
I need to consolidate my local coordinates gotten in 10x10m sub-plots and
to transform them into 100x100m reference. I am doing this by hand in
excel... too much time!!
I need to sum up a reference measure to subplots measure by creating a
loop..
The file "Coord_ha_R" has the columns names "Parcela Sub_parc X_local
Y_local"
The reference file (ref_sub_parc_coord) to sum up values to X and Y_ local
has  the colums name  "sub-parc X Y".

For each "XY_local fields in the "coord_ha_R", I need to sum up the
respective XY  values in the  "ref_sub_parc_coord" file, considering each
sub-parcel.

I'll really apreciate any help, because I have too many plots!!

Thanks
#
Hello K?tia,

That sounds more like a dataframe manipulation problem; though we can 
help. See the code below.

library(dplyr)

coord <- read.table('Coord_ha_R.txt', header = TRUE, fill = TRUE)

plots <- 'ref_sub_parc_coord.txt' %.%
   read.table(header = TRUE) %.%
   mutate(Sub_parc = seq(1,10)) %.%
   merge(coord, by = 'Sub_parc') %.%
   mutate(X_global = X_local + X, Y_global = Y_local + Y)


Is it more or less what you're aiming at? There are certainly many ways 
to come to the same result.
Also you may want to take a look at the dplyr vignette, it's a quick 
read. 
(http://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html)

Cheers,
--
Lo?c Dutrieux
Wageningen University
On 14-07-20 05:51 PM, K?tia Emidio wrote:
#
Hi,

You can solve it using base functions if you prefer:

coord <- read.delim('Coord_ha_R.txt')
ref <- read.delim('ref_sub_parc_coord.txt')

ref <- transform(ref, sub.parc = substr(sub.parc, 9, 10))
coordRef <- merge(coord, ref, by.x = 'Sub_parc', by.y = 'sub.parc')
coordRef <- transform(coordRef, Xref = X_local + X, Yref = Y_local + Y)

Best,

Oscar.
-----------------------------------------------------------------
Oscar Perpi??n Lamigueiro
Dpto. Ingenier?a El?ctrica (ETSIDI-UPM)
Grupo de Sistemas Fotovoltaicos (IES-UPM)
URL: http://oscarperpinan.github.io
Twitter: @oscarperpinan
#
On Sun, 20 Jul 2014, Lo?c Dutrieux wrote:

            
This may work, but is no help at all in understanding R - dplyr is very 
new, and the %.% or %>% syntax is even newer, and very confusing if the 
assumptions made in chaining the steps together are wrong. If you do 
things step-by-step, you can debug them:

coord <- read.table('Coord_ha_R.txt', header = TRUE, fill = TRUE)
str(coord)
% str() describes structure, so showing factors as such;
% head() doesn't do this - see dplyr documentation
ref_sub_parc_coord <- read.table("ref_sub_parc_coord.txt",
  header = TRUE)
str(ref_sub_parc_coord)
# create index
# ref_sub_parc_coord$Sub_parc <- seq(1,10)
# how does mutate() do this properly?
ref_sub_parc_coord$Sub_parc <- 1:nrow(ref_sub_parc_coord)
# merge two data frames
mm <- merge(ref_sub_parc_coord, coord, by = "Sub_parc")
str(mm)
mm$X_global = mm$X_local + mm$X
mm$Y_global = mm$Y_local + mm$Y
str(mm)

By the way, using dplyr:

all.equal(plots, mm)

Please always try to use standard R functions if possible; things like 
dlpyr may be helpful sometimes, but for working scientists simplicity and 
the ability to debug each step are really much more practical.

Roger