I'm an R newbie trying to do an operation that seems like it should be very simple, but after much playing around with the interface and reading "An Introduction to R" I can figure out how to do it. I have two text files. File1 contains: type n dog 2 cat 4 horse 6 File2 contains: type n horse 1 dog 3 cat 9 mouse 11 I want to read both files into R and multiply corresponding values of n so that I end up with: dog 2*3 = 6 cat 4*9 = 36 horse 6*1 = 6 mouse 0*11 = 0 Note that the type rows are not necessarily in the same order in the two files, and that a missing type gets a default n = 0 value. I figure the way to do this is use read.table and do the appropriate manipulation of the factors, but I can't figure out what those manipulations would be. Basically I want to use a factor table as a hash table, but I haven't been able to figure out how to do this, which makes me think I'm not conceptualizing this is the correct R-ish way. Thanks. -- Bill McNeill staff.washington.edu/billmcn/index.shtml [[alternative HTML version deleted]]
?merge Sent from my iPhone On Dec 10, 2008, at 15:17, "Bill McNeill (UW)" <billmcn at u.washington.edu> wrote:> I'm an R newbie trying to do an operation that seems like it should > be very > simple, but after much playing around with the interface and reading > "An > Introduction to R" I can figure out how to do it. > > I have two text files. File1 contains: > > type n > dog 2 > cat 4 > horse 6 > > File2 contains: > > type n > horse 1 > dog 3 > cat 9 > mouse 11 > > I want to read both files into R and multiply corresponding values > of n so > that I end up with: > > dog 2*3 = 6 > cat 4*9 = 36 > horse 6*1 = 6 > mouse 0*11 = 0 > > Note that the type rows are not necessarily in the same order in the > two > files, and that a missing type gets a default n = 0 value. > > I figure the way to do this is use read.table and do the appropriate > manipulation of the factors, but I can't figure out what those > manipulations > would be. Basically I want to use a factor table as a hash table, > but I > haven't been able to figure out how to do this, which makes me think > I'm not > conceptualizing this is the correct R-ish way. > > Thanks. > -- > Bill McNeill > staff.washington.edu/billmcn/index.shtml > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
on 12/10/2008 05:17 PM Bill McNeill (UW) wrote:> I'm an R newbie trying to do an operation that seems like it should be very > simple, but after much playing around with the interface and reading "An > Introduction to R" I can figure out how to do it. > > I have two text files. File1 contains: > > type n > dog 2 > cat 4 > horse 6 > > File2 contains: > > type n > horse 1 > dog 3 > cat 9 > mouse 11 > > I want to read both files into R and multiply corresponding values of n so > that I end up with: > > dog 2*3 = 6 > cat 4*9 = 36 > horse 6*1 = 6 > mouse 0*11 = 0 > > Note that the type rows are not necessarily in the same order in the two > files, and that a missing type gets a default n = 0 value. > > I figure the way to do this is use read.table and do the appropriate > manipulation of the factors, but I can't figure out what those manipulations > would be. Basically I want to use a factor table as a hash table, but I > haven't been able to figure out how to do this, which makes me think I'm not > conceptualizing this is the correct R-ish way. > > Thanks.F3 <- merge(F1, F2, by = "type", all = TRUE)> F3type n.x n.y 1 cat 4 9 2 dog 2 3 3 horse 6 1 4 mouse NA 11 # Convert NA's to 0 F3[is.na(F3)] <- 0> F3type n.x n.y 1 cat 4 9 2 dog 2 3 3 horse 6 1 4 mouse 0 11 F3$prod <- F3$n.x * F3$n.y> F3type n.x n.y prod 1 cat 4 9 36 2 dog 2 3 6 3 horse 6 1 6 4 mouse 0 11 0 See ?merge HTH, Marc Schwartz
Gabor Grothendieck
2008-Dec-11 02:51 UTC
[R] How do I multiply labeled vectors of numbers?
You probably want Marc's solution but just in case note that zoo's merge supports zero fill which means that if you represented the two as zoo objects whose "times" are cat, dog, ... then we would have: File1 <- "type n dog 2 cat 4 horse 6" File2 <- "type n horse 1 dog 3 cat 9 mouse 11" library(zoo) z1 <- read.zoo(textConnection(File1), header = TRUE, FUN = as.character) z2 <- read.zoo(textConnection(File2), header = TRUE, FUN = as.character) z <- merge(z1, z2, fill = 0) zz <- z[,1] * z[,2] Also note that coredata(zz) and time(zz) extract the data and times respectively. On Wed, Dec 10, 2008 at 6:17 PM, Bill McNeill (UW) <billmcn at u.washington.edu> wrote:> I'm an R newbie trying to do an operation that seems like it should be very > simple, but after much playing around with the interface and reading "An > Introduction to R" I can figure out how to do it. > > I have two text files. File1 contains: > > type n > dog 2 > cat 4 > horse 6 > > File2 contains: > > type n > horse 1 > dog 3 > cat 9 > mouse 11 > > I want to read both files into R and multiply corresponding values of n so > that I end up with: > > dog 2*3 = 6 > cat 4*9 = 36 > horse 6*1 = 6 > mouse 0*11 = 0 > > Note that the type rows are not necessarily in the same order in the two > files, and that a missing type gets a default n = 0 value. > > I figure the way to do this is use read.table and do the appropriate > manipulation of the factors, but I can't figure out what those manipulations > would be. Basically I want to use a factor table as a hash table, but I > haven't been able to figure out how to do this, which makes me think I'm not > conceptualizing this is the correct R-ish way. > > Thanks. > -- > Bill McNeill > staff.washington.edu/billmcn/index.shtml > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Seemingly Similar Threads
- using read.table, removing extra quotation mark from a text field? (e.g. ""cat" )
- How do I reload sessions from a non-default directory in OS X?
- How should I denormalise a data frame list of lists column?
- How do I generate one vector for every row of a data frame?
- How do I tapply to a data frame with arbitrary column labels?