[R] Assigning values based on a separate reference (lookup) table

Daniel Malter daniel at umd.edu
Fri Aug 14 20:56:44 CEST 2009


 This is just one suggestion for a solution, and I think there may be much
more elegant ones out there. However, this should work.

1. Reshape your data in long format
2. Merge the table that holds the values you want to multiply your data with
with your reshaped dataset (this may require transformation of the table
that holds the values that are to be multiplied).
3. Perform multiplication
4. Reshape the data in long format back in wide format.

Here is a stepwise example:

##Simulate random data
x=rnorm(36*72)
dim(x)=c(36,72)
x=data.frame(x)
names(x)=1:72

##Reshape data
new.x=reshape(x,idvar="index",ids=row.names(x),direction="long",times=names(
x),timevar="Longitude",varying=list(names(x)))
head(new.x)

##Merge data
Then merge new.x with with the columns of the table you want to multiply
with (see ?merge). This may require extracting data/reshaping of the
multiplier table and/or transforming variables in new.x to make new.x
conformable with table that it is to be merged with.

my.multiplier=data.frame(1:72,rnorm(72))
names(my.multiplier)=c("index","value")

new.x2=merge(new.x,my.multiplier,by.x="Longitude",by.y="index")

##Perform multiplication
names(new.x2)
new.x2$"1"=new.x2$"1"*new.x2$"value"

##Reshape new.x2 back to the initial wide format of the matrix
new.x2=new.x2[,1:3]
new.x=reshape(new.x2,idvar="index",ids=row.names(x),direction="wide",times=n
ames(x),timevar="Longitude",varying=list(names(x)))
head(new.x)

HTH,
Daniel



-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von Steve Murray
Gesendet: Friday, August 14, 2009 1:49 PM
An: r-help at r-project.org
Betreff: [R] Assigning values based on a separate reference (lookup) table


Dear R Users,

I have a data frame of 360 rows by 720 columns (259200 values). For each
value in this grid I am hoping to apply an equation to, to generate a new
grid. One of the parts of the equation (called 'p') relies on reading from a
separate reference table. This is Table 4 at:
http://www.fao.org/docrep/s2022e/s2022e07.htm#3.1.3%20blaney%20criddle%20met
hod (scroll down a little).

Therefore, 'p' relies on the latitude of the values in the initial 360 x 720
data frame. The row names of the data frame contain the latitude values and
these range from between 89.75 to -89.75 (the latter being South of the
Equator).

My question is, how do I go about forming a loop to read each of the 259200
values and assign it a 'p' value (from the associated reference table),
based on it's latitude?

My thinking was to do a series of 'if' statements, but this soon got very,
very messy - any ideas which get the job done (and aren't a riddle to
follow), would be most welcome.

Many thanks for any advice,

Steve


_________________________________________________________________

[[elided Hotmail spam]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list