[R] How do I multiply labeled vectors of numbers?

Jim Holtman jholtman at gmail.com
Thu Dec 11 00:20:39 CET 2008


?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
> http://staff.washington.edu/billmcn/index.shtml
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> 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