Resending after fixing a mistake in the earlier email ... sorry for the
confusion.

******
Dear R-Users,
I need to lookup values from a 2-d table using the row names and column
names as indices. I was wondering if there's a way to do this without an
explicit loop.

Example:
#x is the 2-d table that holds the values

x <- matrix(rnorm(26*12),nrow=26)

rownames(x) <- letters

colnames(x) <- month.name


#y is a data frame that has the "keys" I want to use as indices into x

y <- data.frame(ltrs=sample(letters,5),mnths=sample(month.name,5),values=0)


#I want to fill in the "values" column using the "ltrs" and "mnths" columns
as keys to look up

# the associated value from x

#One way to do this is with a FOR loop


for (i in 1:nrow(y)) {y$values[i] <- x[as.character(y$ltrs[i]),as.character(
y$mnths[i])]}


My question: Is there a more efficient way (e.g., one without using an
explicit loop) to do this?


Thanks in advance!


-Rama Ramakrishnan

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to