Re: [R] creating matrices from lists
On 09/21/2010 05:02 AM, Gregory Ryslik wrote: Hi, I think I've found away around that issue. The following works. If this method is inefficient and one has something faster, I'll appreciate it though! lapply(mylist, function(x) as.numeric(as.character(x))) You could avoid making them factors in the first place Otherwise, the common trick is as.numeric(levels(x))[x] or, if you are sure that the levels are always 0/1, c(0,1)[x]. Of course if that is true you could just accept the 1/2 solution and subtract 1 at the end. (But beware that you might have one element as a factor with a single level 1, and that would get treated exactly the same as a factor with only 0...) -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ 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.
Re: [R] creating matrices from lists
On Sep 20, 2010, at 10:12 PM, Gregory Ryslik wrote: Hi, I have a list of 5 main elements where each main element has 247 atomic entries either 0 or 1. I need to get this into a 247x5 matrix so I do do.call(cbind, mylist). However, it renumbers 0 to a 1 and the 1 to a 2 so that my matrix is filled with 1's and 2's. If I had such a problem I would see if this were more effective: matrix(unlist(mylist), length(mylist[[1]]) ) (There is the column major order default of R matrices.) I understand I can fix it in this case by doing a replace but I would like to avoid that step. Further, sometimes, my list entries will be from 0 to n. I don't want to have to always renumber all the possibilities. I'm not quite sure why this is going on because when I build the following: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) and execute do.call(cbind, l) it works just fine and there is no renumbering! I wouldn't have expected it either, but you only provided an example of what did work. My attempt at reproducing your problem also failed: ll - list(a=c(1,0,1,1,0), b=c(0,1,0,0,1) ) do.call(cbind, ll) a b [1,] 1 0 [2,] 0 1 [3,] 1 0 [4,] 1 0 [5,] 0 1 I am wondering if you are dealing with factors and have not looked at your list with str(). Haven't tried my method above to see what would happen in tat instance. Thanks, Greg [[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. David Winsemius, MD West Hartford, CT __ 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.
Re: [R] creating matrices from lists
Hm, Now that you mention it, I believe they are factors. They just appeared as 0 or 1 so I treated them as numbers. Once I found out they were factors I tried using the as.numeric() but that makes it 1 or 2 as before. How do I actually make it keep the factor number? Thanks, Greg On Sep 20, 2010, at 10:28 PM, David Winsemius wrote: On Sep 20, 2010, at 10:12 PM, Gregory Ryslik wrote: Hi, I have a list of 5 main elements where each main element has 247 atomic entries either 0 or 1. I need to get this into a 247x5 matrix so I do do.call(cbind, mylist). However, it renumbers 0 to a 1 and the 1 to a 2 so that my matrix is filled with 1's and 2's. If I had such a problem I would see if this were more effective: matrix(unlist(mylist), length(mylist[[1]]) ) (There is the column major order default of R matrices.) I understand I can fix it in this case by doing a replace but I would like to avoid that step. Further, sometimes, my list entries will be from 0 to n. I don't want to have to always renumber all the possibilities. I'm not quite sure why this is going on because when I build the following: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) and execute do.call(cbind, l) it works just fine and there is no renumbering! I wouldn't have expected it either, but you only provided an example of what did work. My attempt at reproducing your problem also failed: ll - list(a=c(1,0,1,1,0), b=c(0,1,0,0,1) ) do.call(cbind, ll) a b [1,] 1 0 [2,] 0 1 [3,] 1 0 [4,] 1 0 [5,] 0 1 I am wondering if you are dealing with factors and have not looked at your list with str(). Haven't tried my method above to see what would happen in tat instance. Thanks, Greg [[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. David Winsemius, MD West Hartford, CT __ 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.
Re: [R] creating matrices from lists
Hi Gregory, May be this? # some data set.seed(123) x - factor(sample(0:1, 20, TRUE)) x # [1] 0 1 0 1 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 1 # Levels: 0 1 as.numeric(as.factor(x)) # [1] 1 2 1 2 2 1 2 2 2 1 2 1 2 2 1 2 1 1 1 2 as.numeric(as.character(x)) [1] 0 1 0 1 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 1 HTH, Jorge On Mon, Sep 20, 2010 at 10:40 PM, Gregory Ryslik wrote: Hm, Now that you mention it, I believe they are factors. They just appeared as 0 or 1 so I treated them as numbers. Once I found out they were factors I tried using the as.numeric() but that makes it 1 or 2 as before. How do I actually make it keep the factor number? Thanks, Greg On Sep 20, 2010, at 10:28 PM, David Winsemius wrote: On Sep 20, 2010, at 10:12 PM, Gregory Ryslik wrote: Hi, I have a list of 5 main elements where each main element has 247 atomic entries either 0 or 1. I need to get this into a 247x5 matrix so I do do.call(cbind, mylist). However, it renumbers 0 to a 1 and the 1 to a 2 so that my matrix is filled with 1's and 2's. If I had such a problem I would see if this were more effective: matrix(unlist(mylist), length(mylist[[1]]) ) (There is the column major order default of R matrices.) I understand I can fix it in this case by doing a replace but I would like to avoid that step. Further, sometimes, my list entries will be from 0 to n. I don't want to have to always renumber all the possibilities. I'm not quite sure why this is going on because when I build the following: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) and execute do.call(cbind, l) it works just fine and there is no renumbering! I wouldn't have expected it either, but you only provided an example of what did work. My attempt at reproducing your problem also failed: ll - list(a=c(1,0,1,1,0), b=c(0,1,0,0,1) ) do.call(cbind, ll) a b [1,] 1 0 [2,] 0 1 [3,] 1 0 [4,] 1 0 [5,] 0 1 I am wondering if you are dealing with factors and have not looked at your list with str(). Haven't tried my method above to see what would happen in tat instance. Thanks, Greg [[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. David Winsemius, MD West Hartford, CT __ 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. [[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.
Re: [R] creating matrices from lists
On Sep 20, 2010, at 10:28 PM, David Winsemius wrote: On Sep 20, 2010, at 10:12 PM, Gregory Ryslik wrote: Hi, I have a list of 5 main elements where each main element has 247 atomic entries either 0 or 1. I need to get this into a 247x5 matrix so I do do.call(cbind, mylist). However, it renumbers 0 to a 1 and the 1 to a 2 so that my matrix is filled with 1's and 2's. If I had such a problem I would see if this were more effective: matrix(unlist(mylist), length(mylist[[1]]) ) (There is the column major order default of R matrices.) I understand I can fix it in this case by doing a replace but I would like to avoid that step. Further, sometimes, my list entries will be from 0 to n. I don't want to have to always renumber all the possibilities. I'm not quite sure why this is going on because when I build the following: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) and execute do.call(cbind, l) it works just fine and there is no renumbering! I wouldn't have expected it either, but you only provided an example of what did work. My attempt at reproducing your problem also failed: ll - list(a=c(1,0,1,1,0), b=c(0,1,0,0,1) ) do.call(cbind, ll) a b [1,] 1 0 [2,] 0 1 [3,] 1 0 [4,] 1 0 [5,] 0 1 I am wondering if you are dealing with factors and have not looked at your list with str(). Haven't tried my method above to see what would happen in tat instance. Does seem possible that is the issue: do.call(cbind, list(factor(c(0,1,0,1,1,1,0)),b=factor(c(0,0,0,1,1,1,1 b [1,] 1 1 [2,] 2 1 [3,] 1 1 [4,] 2 2 [5,] 2 2 [6,] 2 2 [7,] 1 2 -- David. __ 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.
Re: [R] creating matrices from lists
Hi, I think I've found away around that issue. The following works. If this method is inefficient and one has something faster, I'll appreciate it though! lapply(mylist, function(x) as.numeric(as.character(x))) Cheers, Greg On Sep 20, 2010, at 10:48 PM, David Winsemius wrote: On Sep 20, 2010, at 10:28 PM, David Winsemius wrote: On Sep 20, 2010, at 10:12 PM, Gregory Ryslik wrote: Hi, I have a list of 5 main elements where each main element has 247 atomic entries either 0 or 1. I need to get this into a 247x5 matrix so I do do.call(cbind, mylist). However, it renumbers 0 to a 1 and the 1 to a 2 so that my matrix is filled with 1's and 2's. If I had such a problem I would see if this were more effective: matrix(unlist(mylist), length(mylist[[1]]) ) (There is the column major order default of R matrices.) I understand I can fix it in this case by doing a replace but I would like to avoid that step. Further, sometimes, my list entries will be from 0 to n. I don't want to have to always renumber all the possibilities. I'm not quite sure why this is going on because when I build the following: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) and execute do.call(cbind, l) it works just fine and there is no renumbering! I wouldn't have expected it either, but you only provided an example of what did work. My attempt at reproducing your problem also failed: ll - list(a=c(1,0,1,1,0), b=c(0,1,0,0,1) ) do.call(cbind, ll) a b [1,] 1 0 [2,] 0 1 [3,] 1 0 [4,] 1 0 [5,] 0 1 I am wondering if you are dealing with factors and have not looked at your list with str(). Haven't tried my method above to see what would happen in tat instance. Does seem possible that is the issue: do.call(cbind, list(factor(c(0,1,0,1,1,1,0)),b=factor(c(0,0,0,1,1,1,1 b [1,] 1 1 [2,] 2 1 [3,] 1 1 [4,] 2 2 [5,] 2 2 [6,] 2 2 [7,] 1 2 -- David. [[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.