[R] set dataframe field value from lookup table

Jon Erik Ween jween at klaru-baycrest.on.ca
Thu Dec 9 15:34:29 CET 2010


Hi

This is (hopefully) a bit more cogent phrasing of a previous post. I'm
trying to compute a z-score to rows in a large dataframe based on values in
another dataframe. Here's the script (that does not work). 2 questons,

1) Anyone know of a more elegant way to calculate the "rounded" age value
than the nested ifelse's I've used?

2) how to reference the lookup table based on computed indices?

Thanks

Jon

# Define tables
DSTzlook <-
read.table("/Users/jween/Documents/ResearchProjects/ABC/data/DSTz.txt",
header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
df<-stroke

# Compute rounded age.
df$Agetmp
<-ifelse(df$Age>=89,89,ifelse(df$Age>=84,84,ifelse(df$Age>=79,79,ifelse(df$Age>=74,74,ifelse(df$Age>=69,69,ifelse(df$Age>=64,64,ifelse(df$Age>=54,54,ifelse(df$Age>=44,44,ifelse(df$Age>=34,34,ifelse(df$Age>=29,29,ifelse(df$Age>=24,24,ifelse(df$Age>=19,19,17))))))))))))

# Reference the lookup table based on computed indices
df$DSTz
<-DSTzlook[which(DSTzlook[,1]==df$Agetmp),which(DSTzlook[1,]==df$DSF+df$DSB)]

# Cleanup
#rm(df)
#df$Agetmp<-NULL
-- 
View this message in context: http://r.789695.n4.nabble.com/set-dataframe-field-value-from-lookup-table-tp3080245p3080245.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list