[R] suggested method to transform list to a matrix

R. Michael Weylandt michael.weylandt at gmail.com
Thu Apr 19 22:35:41 CEST 2012


Use matrix subsetting like this:

x <- matrix(1:9,3)
rownames(x) <- letters[1:3]
colnames(x) <- LETTERS[1:3]

print(x)

usrs <- c("a","b","a")
vars <- c("C","C","A")
counts <- c(10,11,12)

x[cbind(usrs, vars)] <- counts

print(x)

Hope this helps,
Michael

On Thu, Apr 19, 2012 at 1:48 PM, Tim Stutt <tim at ischool.berkeley.edu> wrote:
> I have data in the following list format:
>
> USER,VARIABLE,COUNT
> user1, var1, 3
> user1, var2, 4
> user2, var1, 7
> userN, var12, 5
>
> And would like to have it format as a matrix:
>
>                var1            var2            var12
> user1   3               4
> user2   7
> userN                                   5
>
> What is the suggested method to do this for 1 million rows, with 12 variables and ~ 4,000 unique users?
>
> Thank you,
>
> Tim Stutt
> tim at ischool.berkeley.edu
>
>
>
>        [[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