From: Alice Wines <aabroadh_at_ncsu.edu>

Date: Sat, 30 Apr 2011 19:18:50 -0400

A, 21_2 3.3 2.5 67.2 44.3 53 66

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. Received on Sun 01 May 2011 - 03:18:08 GMT

Date: Sat, 30 Apr 2011 19:18:50 -0400

Hello all,

I have a quandry I have been scratching my head about for a while. I've searched the manual and the web and have not been able to find an acceptable result, so I am hoping for some help.

I have two data frames and I want to index into the first using the second, and replace the specific values I have indexed with more values from the second data.frame. I can do this using a loop, but I wanted a quicker solution with no loops involved.

Although my data set is much larger than this, a small example of what I am trying to do is as follows:

df1 <- data.frame(rows=c("A","B","C", "B", "C", "A"),
columns=c("21_2", "22_2", "23_2", "21_2", "22_2", "23_2"),
values=c(3.3, 2.5, 67.2, 44.3, 53, 66))

df2 <- data.frame(matrix(rep(NA, length(df1$values)),nrow=3, ncol=3))
names(df2) <- c("21_2", "22_2", "23_2")

row.names(df2) <- c("A", "B", "C")

*> df1
*

rows columns values

1 A 21_2 3.3 2 B 22_2 2.5 3 C 23_2 67.2 4 B 21_2 44.3 5 C 22_2 53.0 6 A 23_2 66.0

*> df2
*

21_2 22_2 23_2

A NA NA NA

B NA NA NA

C NA NA NA

When I tried mapply, the code I used was something like what follows:

but it yields the following strange result

21_2 22_2 23_2 <NA> <NA> <NA> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

A, 21_2 3.3 2.5 67.2 44.3 53 66

What I want to see is the following:

*>df3
*

21_2 22_2 23_2

A 3.3 NA 66.0

B 44.3 2.5 NA

C NA 53.0 67.2

I will greatly appreciate any help that can be given as I am completely bamboozled by this problem and although I found many useful things in my search for an answer, I did not find out how to do this.

Thanks,

Alice

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. Received on Sun 01 May 2011 - 03:18:08 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Thu 05 May 2011 - 07:00:04 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*