Re: [R] removing NA from a data frame
* Adaikalavan Ramasamy [EMAIL PROTECTED] [2006-03-19 04:51:19 +]: 1) R-help is designed for and by unpaid volunteers. Therefore sometimes RTFM without page reference is quite acceptable. I am an unpaid volunteer maintainer of CLISP (http://clisp.cons.org). I often answer questions with specific link to the CLISP FAQ (which really is just that - the list of the frequently asked questions). If you do not feel like answering a question, it is perfectly fine with me, I do not think that anyone owes me anything. All I am asking is that if you do decide to answer, please make your answer immediately useful, i.e., not requiring learning all the manual by heart (a specific manual section is perfectly fine though). Thanks. PS. Many thanks to Ben Bolker and Haifeng Xie for their help! PPS. how do I figure out the number of rows in a data.frame? is length(attr(X,row.names)) the right way? -- Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 4 (Stentz) http://www.camera.org http://pmw.org.il http://www.memri.org http://www.jihadwatch.org http://www.dhimmi.com Are you smart enough to use Lisp? __ R-help@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
Re: [R] removing NA from a data frame
If you do not feel like answering a question, it is perfectly fine with me, I do not think that anyone owes me anything. All I am asking is that if you do decide to answer, please make your answer immediately useful, i.e., not requiring learning all the manual by heart (a specific manual section is perfectly fine though). Thanks. But do you not owe the list the courtesy of first making a reasonable attempt on your own? PPS. how do I figure out the number of rows in a data.frame? is length(attr(X,row.names)) the right way? help.search(number of rows) immediately gets you your answer! -- Bert Gunter Genentech __ R-help@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
Re: [R] removing NA from a data frame
* Berton Gunter [EMAIL PROTECTED] [2006-03-20 09:42:49 -0800]: If you do not feel like answering a question, it is perfectly fine with me, I do not think that anyone owes me anything. All I am asking is that if you do decide to answer, please make your answer immediately useful, i.e., not requiring learning all the manual by heart (a specific manual section is perfectly fine though). Thanks. But do you not owe the list the courtesy of first making a reasonable attempt on your own? I do. Nevertheless, please remember that a reasonable attempt means different things for a newbie and for an expert. PPS. how do I figure out the number of rows in a data.frame? is length(attr(X,row.names)) the right way? help.search(number of rows) immediately gets you your answer! thanks! -- Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 4 (Stentz) http://www.mideasttruth.com http://ffii.org http://www.memri.org http://www.iris.org.il http://pmw.org.il http://www.palestinefacts.org All extremists should be taken out and shot. __ R-help@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
Re: [R] removing NA from a data frame
You might find the 2nd part of the following response useful https://stat.ethz.ch/pipermail/r-help/2006-March/090611.html And if you want to RTFM, I guess sections 2.5, 2.7, 5.1, 5.2 of http://cran.r-project.org/doc/manuals/R-intro.html might be useful. PS: 1) R-help is designed for and by unpaid volunteers. Therefore sometimes RTFM without page reference is quite acceptable. 2) Similar question often gets repeated over and over the list. It might be useful to search http://finzi.psych.upenn.edu/nmz.html first. On Fri, 2006-03-17 at 16:17 -0500, Sam Steingold wrote: * Francisco J. Zagmutt [EMAIL PROTECTED] [2006-03-17 21:09:48 +]: Go to the help menu- manuals in pdf and select An Introduction to R. After you read that document you will be able to answer your questions :-) I did. I still need help. The matter is not so much with getting things done (I can probably write the code - although I would rather not) as with not reinventing the wheel. PS. next time you decide to answer my question with RTFM, please also include the number of the page that answers my specific question. __ R-help@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
[R] removing NA from a data frame
Hi, It appears that deal does not support missing values (NA), so I need to remove them (NAs) from my data frame. how do I do this? (I am very new to R, so a detailed step-by-step explanation with code samples would be nice). Some columns (variables) have quite a few NAs, so I would rather drop the whole column than sacrifice all the rows (observations) which have NA in that column. How do I remove a column from a data frame? Thanks! -- Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 4 (Stentz) http://ffii.org http://www.mideasttruth.com http://pmw.org.il http://www.dhimmi.com http://www.honestreporting.com http://www.jihadwatch.org Don't hit a man when he's down -- kick him; it's easier. __ R-help@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
Re: [R] removing NA from a data frame
Hi Sam If you are new to R it will definitively pay off to start from the basics. Go to the help menu- manuals in pdf and select An Introduction to R. After you read that document you will be able to answer your questions :-) Good luck! Francisco From: Sam Steingold [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject: [R] removing NA from a data frame Date: Fri, 17 Mar 2006 15:17:51 -0500 Hi, It appears that deal does not support missing values (NA), so I need to remove them (NAs) from my data frame. how do I do this? (I am very new to R, so a detailed step-by-step explanation with code samples would be nice). Some columns (variables) have quite a few NAs, so I would rather drop the whole column than sacrifice all the rows (observations) which have NA in that column. How do I remove a column from a data frame? Thanks! -- Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 4 (Stentz) http://ffii.org http://www.mideasttruth.com http://pmw.org.il http://www.dhimmi.com http://www.honestreporting.com http://www.jihadwatch.org Don't hit a man when he's down -- kick him; it's easier. __ R-help@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 __ R-help@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
Re: [R] removing NA from a data frame
* Francisco J. Zagmutt [EMAIL PROTECTED] [2006-03-17 21:09:48 +]: Go to the help menu- manuals in pdf and select An Introduction to R. After you read that document you will be able to answer your questions :-) I did. I still need help. The matter is not so much with getting things done (I can probably write the code - although I would rather not) as with not reinventing the wheel. PS. next time you decide to answer my question with RTFM, please also include the number of the page that answers my specific question. -- Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 4 (Stentz) http://www.jihadwatch.org http://www.camera.org http://www.mideasttruth.com http://www.memri.org http://www.palestinefacts.org http://www.savegushkatif.org Type louder, please. __ R-help@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
Re: [R] removing NA from a data frame
Sam Steingold sds at podval.org writes: Hi, It appears that deal does not support missing values (NA), so I need to remove them (NAs) from my data frame. how do I do this? (I am very new to R, so a detailed step-by-step explanation with code samples would be nice). If you wanted to remove rows with NAs from data frame X na.omit(X) would do it. In this case I think X[!sapply(X,function(z)any(is.na(z)))] should work, although I haven't tested it. function(z)any(is.na(z)) looks for any NA values sapply applies the function to each element in the list (= column in the data frame) and returns a vector ! negates the logical vector [] picks the appropriate elements (=columns) out of the list (=dataframe) I haven't tested it. Conceivably X[!sapply(is.na(X),any)] or X[sapply(!is.na(X),all)] would work too, although I'm not sure. Ben __ R-help@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
Re: [R] removing NA from a data frame
If I understand it correctly, something like this should do what you want x[!apply(x, 1, function(y) any(is.na(y)), ] where x is the dataframe in question. Hope that helps. Kevin - Original Message - From: Ben Bolker [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Friday, March 17, 2006 10:33 PM Subject: Re: [R] removing NA from a data frame Sam Steingold sds at podval.org writes: Hi, It appears that deal does not support missing values (NA), so I need to remove them (NAs) from my data frame. how do I do this? (I am very new to R, so a detailed step-by-step explanation with code samples would be nice). If you wanted to remove rows with NAs from data frame X na.omit(X) would do it. In this case I think X[!sapply(X,function(z)any(is.na(z)))] should work, although I haven't tested it. function(z)any(is.na(z)) looks for any NA values sapply applies the function to each element in the list (= column in the data frame) and returns a vector ! negates the logical vector [] picks the appropriate elements (=columns) out of the list (=dataframe) I haven't tested it. Conceivably X[!sapply(is.na(X),any)] or X[sapply(!is.na(X),all)] would work too, although I'm not sure. Ben __ R-help@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 __ R-help@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
Re: [R] removing NA from a data frame
Haifeng Xie xieh at wmin.ac.uk writes: If I understand it correctly, something like this should do what you want x[!apply(x, 1, function(y) any(is.na(y)), ] where x is the dataframe in question. Hope that helps. Kevin I believe he wants to remove *columns* with NAs, not rows (if he wanted to remove rows then complete.cases(x) would work) x[,!apply(x,2,function(y)any(is.na(y))] or x[,!apply(is.na(x),2,any)] (I wasn't sure one could apply() on columns of a data frame -- I'm always a little certain about the matrix - data.frame mapping -- but I tried it and you can. Now that I think about it, I don't know why I thought you couldn't. apply() on rows would be more likely to be problematic.) is.na() turns a data frame into a matrix, so x[!sapply(is.na(x),any)] *does not* work. x[complete.cases(t(is.na(x)))] or t(na.omit(t(X))) both do, if your data frame is all numeric. Ben __ R-help@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