I have some matrices stored as elements in a list that I am working with. On example is provided below as TP[[18]]> TP[[18]]level2 level1 1 2 3 4 1 79 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 Now, using prop.table on this gives> prop.table(TP[[18]],1)level2 level1 1 2 3 4 1 1 0 0 0 2 3 4 It is important for the zero's to retain their position as this matrix will subsequently be used in some matrix multiplication and hence, must be of dimension 4 by 4 so that is it conformable for multiplcation with another matrix. In looking at the structure of the object resulting from prop.table I see NaNs, and so I can do this> rr <- TP[[18]] > rr[is.na(rr)] <- 0 > rrlevel2 level1 1 2 3 4 1 79 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 This is exactly what I want for each matrix. But, I have multiple matrices stored within the list that need to be changed and so I am trying to resolve this via lapply, but something is awry (namely the user), but I could use a little help. I was thinking the following function should work, but it doesn't. It reduces each matrix within the list to a 0. PP <- lapply(TP, function(x) x[is.na(x)] <- 0) Am I missing something obvious? Harold [[alternative HTML version deleted]]
you need to return x in the function within lapply(), e.g., something like lapply(TP, function(x) { x[is.na(x)] <- 0; x }) I hope it works. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Doran, Harold" <HDoran at air.org> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, January 24, 2007 4:40 PM Subject: [R] Replace missing values in lapply>I have some matrices stored as elements in a list that I am working > with. On example is provided below as TP[[18]] > >> TP[[18]] > level2 > level1 1 2 3 4 > 1 79 0 0 0 > 2 0 0 0 0 > 3 0 0 0 0 > 4 0 0 0 0 > > Now, using prop.table on this gives > >> prop.table(TP[[18]],1) > level2 > level1 1 2 3 4 > 1 1 0 0 0 > 2 > 3 > 4 > > It is important for the zero's to retain their position as this > matrix > will subsequently be used in some matrix multiplication and hence, > must > be of dimension 4 by 4 so that is it conformable for multiplcation > with > another matrix. > > In looking at the structure of the object resulting from prop.table > I > see NaNs, and so I can do this > >> rr <- TP[[18]] >> rr[is.na(rr)] <- 0 >> rr > level2 > level1 1 2 3 4 > 1 79 0 0 0 > 2 0 0 0 0 > 3 0 0 0 0 > 4 0 0 0 0 > > This is exactly what I want for each matrix. But, I have multiple > matrices stored within the list that need to be changed and so I am > trying to resolve this via lapply, but something is awry (namely the > user), but I could use a little help. > > I was thinking the following function should work, but it doesn't. > It > reduces each matrix within the list to a 0. > > PP <- lapply(TP, function(x) x[is.na(x)] <- 0) > > Am I missing something obvious? > > Harold > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Perfect, thxs> -----Original Message----- > From: Dimitris Rizopoulos > [mailto:dimitris.rizopoulos at med.kuleuven.be] > Sent: Wednesday, January 24, 2007 10:49 AM > To: Doran, Harold > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Replace missing values in lapply > > you need to return x in the function within lapply(), e.g., > something like > > lapply(TP, function(x) { x[is.na(x)] <- 0; x }) > > > I hope it works. > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/(0)16/336899 > Fax: +32/(0)16/337015 > Web: http://med.kuleuven.be/biostat/ > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > > ----- Original Message ----- > From: "Doran, Harold" <HDoran at air.org> > To: <r-help at stat.math.ethz.ch> > Sent: Wednesday, January 24, 2007 4:40 PM > Subject: [R] Replace missing values in lapply > > > >I have some matrices stored as elements in a list that I am working > > with. On example is provided below as TP[[18]] > > > >> TP[[18]] > > level2 > > level1 1 2 3 4 > > 1 79 0 0 0 > > 2 0 0 0 0 > > 3 0 0 0 0 > > 4 0 0 0 0 > > > > Now, using prop.table on this gives > > > >> prop.table(TP[[18]],1) > > level2 > > level1 1 2 3 4 > > 1 1 0 0 0 > > 2 > > 3 > > 4 > > > > It is important for the zero's to retain their position as this > > matrix > > will subsequently be used in some matrix multiplication and hence, > > must > > be of dimension 4 by 4 so that is it conformable for multiplcation > > with > > another matrix. > > > > In looking at the structure of the object resulting from prop.table > > I > > see NaNs, and so I can do this > > > >> rr <- TP[[18]] > >> rr[is.na(rr)] <- 0 > >> rr > > level2 > > level1 1 2 3 4 > > 1 79 0 0 0 > > 2 0 0 0 0 > > 3 0 0 0 0 > > 4 0 0 0 0 > > > > This is exactly what I want for each matrix. But, I have multiple > > matrices stored within the list that need to be changed and so I am > > trying to resolve this via lapply, but something is awry (namely the > > user), but I could use a little help. > > > > I was thinking the following function should work, but it doesn't. > > It > > reduces each matrix within the list to a 0. > > > > PP <- lapply(TP, function(x) x[is.na(x)] <- 0) > > > > Am I missing something obvious? > > > > Harold > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > > > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm > >
I wonder if a list of matrices is the best representation? Do your matrices all have the same dimension as in: TP <- list(matrix(c(1:3, NA), 2), matrix(c(NA, 1:3), 2)) # Then you could consider representing them as an array: TPa <- array(unlist(TP), c(2,2,2)) # in which case its just TPa[is.na(TPa)] <- 0 TPa On 1/24/07, Doran, Harold <HDoran at air.org> wrote:> I have some matrices stored as elements in a list that I am working > with. On example is provided below as TP[[18]] > > > TP[[18]] > level2 > level1 1 2 3 4 > 1 79 0 0 0 > 2 0 0 0 0 > 3 0 0 0 0 > 4 0 0 0 0 > > Now, using prop.table on this gives > > > prop.table(TP[[18]],1) > level2 > level1 1 2 3 4 > 1 1 0 0 0 > 2 > 3 > 4 > > It is important for the zero's to retain their position as this matrix > will subsequently be used in some matrix multiplication and hence, must > be of dimension 4 by 4 so that is it conformable for multiplcation with > another matrix. > > In looking at the structure of the object resulting from prop.table I > see NaNs, and so I can do this > > > rr <- TP[[18]] > > rr[is.na(rr)] <- 0 > > rr > level2 > level1 1 2 3 4 > 1 79 0 0 0 > 2 0 0 0 0 > 3 0 0 0 0 > 4 0 0 0 0 > > This is exactly what I want for each matrix. But, I have multiple > matrices stored within the list that need to be changed and so I am > trying to resolve this via lapply, but something is awry (namely the > user), but I could use a little help. > > I was thinking the following function should work, but it doesn't. It > reduces each matrix within the list to a 0. > > PP <- lapply(TP, function(x) x[is.na(x)] <- 0) > > Am I missing something obvious? > > Harold > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
I hadn't thought of that. I use the following at one point in my program tmp <- with(data, tapply(variable, index, table)) Which returns a list. So, I just went with it for the rest of my program. I'm changing code now to arrays, I think you're right and this may be a better representation. I need to walk through this and see what turns up. Thanks for the recommendation.> -----Original Message----- > From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] > Sent: Wednesday, January 24, 2007 11:06 AM > To: Doran, Harold > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Replace missing values in lapply > > I wonder if a list of matrices is the best representation? > Do your matrices all have the same dimension as in: > > TP <- list(matrix(c(1:3, NA), 2), matrix(c(NA, 1:3), 2)) > > # Then you could consider representing them as an array: > > TPa <- array(unlist(TP), c(2,2,2)) > > # in which case its just > > TPa[is.na(TPa)] <- 0 > TPa > > > On 1/24/07, Doran, Harold <HDoran at air.org> wrote: > > I have some matrices stored as elements in a list that I am working > > with. On example is provided below as TP[[18]] > > > > > TP[[18]] > > level2 > > level1 1 2 3 4 > > 1 79 0 0 0 > > 2 0 0 0 0 > > 3 0 0 0 0 > > 4 0 0 0 0 > > > > Now, using prop.table on this gives > > > > > prop.table(TP[[18]],1) > > level2 > > level1 1 2 3 4 > > 1 1 0 0 0 > > 2 > > 3 > > 4 > > > > It is important for the zero's to retain their position as > this matrix > > will subsequently be used in some matrix multiplication and hence, > > must be of dimension 4 by 4 so that is it conformable for > > multiplcation with another matrix. > > > > In looking at the structure of the object resulting from > prop.table I > > see NaNs, and so I can do this > > > > > rr <- TP[[18]] > > > rr[is.na(rr)] <- 0 > > > rr > > level2 > > level1 1 2 3 4 > > 1 79 0 0 0 > > 2 0 0 0 0 > > 3 0 0 0 0 > > 4 0 0 0 0 > > > > This is exactly what I want for each matrix. But, I have multiple > > matrices stored within the list that need to be changed and so I am > > trying to resolve this via lapply, but something is awry > (namely the > > user), but I could use a little help. > > > > I was thinking the following function should work, but it > doesn't. It > > reduces each matrix within the list to a 0. > > > > PP <- lapply(TP, function(x) x[is.na(x)] <- 0) > > > > Am I missing something obvious? > > > > Harold > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > >