Re: [R] NROW doesn't equal length(x)
Your first problem is that you aren't using paste() properly: print out paste(ct3[, 1:2]) and take a look at it. This works: apply(head(ct3[,1:2]),1,paste,collapse = ) You also have the format argument to POSIXct wrong. See ?strptime for details. So the whole line (if you want to do it in one) would be something like this: v = xts(ct3[,3], as.POSIXct(apply(ct3[, 1:2],1, paste, collapse = ), format = %m/%d/%Y %H:%M:%S)) head(v) 2011-02-22 09:31:13 19.46 2011-02-22 09:31:28 19.50 2011-02-22 09:31:43 19.55 2011-02-22 09:31:58 19.59 2011-02-22 09:32:13 19.67 2011-02-22 09:32:28 19.68 Michael PS -- It's best practice to cc the list as well as me in replies so that this gets archived. On Tue, Nov 1, 2011 at 5:37 PM, Muhammad Abuizzah izzah...@yahoo.com wrote: I attached a dput of my data file which I am trying to transform to xts. The name of my object is ct3, I am putting the generated info into v The code I used to convert it to xts is as follows: v = xts(ct3[,3], as.POSIXct(paste (ct3 [,1:2]), format = %MM/%DD/% %H:%M:%:S) I would appreciate any help in converting this data frame into xts. I am not sure is the NROW issue is the reason behind the failure or is it the data formate thanks - Original Message - From: R. Michael Weylandt michael.weyla...@gmail.com michael.weyla...@gmail.com To: Muhammad Abuizzah izzah...@yahoo.com Cc: r-help@R-project.org r-help@r-project.org Sent: Thursday, October 27, 2011 3:23 PM Subject: Re: [R] NROW doesn't equal length(x) Data frame is list internally so length(df) = ncol(df) M On Oct 27, 2011, at 2:44 PM, Muhammad Abuizzah izzah...@yahoo.com wrote: Hi, I am converting a data.frame to xts. the data.frame is 4 columns and 1000 rows. I get a message that NROW (x) must match length(order.by) class is data.frame, mode is list when I run dim(x) # I get 1000 4 #which is consistent with 1000 rows and 4 columns NROW (x) # I get 1000 # which is the right answer When I run length on each of columns in x separately using the $ I get 1000, which is the right number too. So length on each of the columns individually gives me the right answer, but length on the data.frame gives me the number of columns instead of the number of rows, is there an explanation thanks __ 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. __ 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] NROW doesn't equal length(x)
I perhaps made that a little too complicated: this will also work: v2 = xts(ct3[,3], as.POSIXct(paste(ct3[,1], ct3[,2]), format = %m/%d/%Y %H:%M:%S)) identical(v, v2) TRUE On Tue, Nov 1, 2011 at 6:06 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: Your first problem is that you aren't using paste() properly: print out paste(ct3[, 1:2]) and take a look at it. This works: apply(head(ct3[,1:2]),1,paste,collapse = ) You also have the format argument to POSIXct wrong. See ?strptime for details. So the whole line (if you want to do it in one) would be something like this: v = xts(ct3[,3], as.POSIXct(apply(ct3[, 1:2],1, paste, collapse = ), format = %m/%d/%Y %H:%M:%S)) head(v) 2011-02-22 09:31:13 19.46 2011-02-22 09:31:28 19.50 2011-02-22 09:31:43 19.55 2011-02-22 09:31:58 19.59 2011-02-22 09:32:13 19.67 2011-02-22 09:32:28 19.68 Michael PS -- It's best practice to cc the list as well as me in replies so that this gets archived. On Tue, Nov 1, 2011 at 5:37 PM, Muhammad Abuizzah izzah...@yahoo.com wrote: I attached a dput of my data file which I am trying to transform to xts. The name of my object is ct3, I am putting the generated info into v The code I used to convert it to xts is as follows: v = xts(ct3[,3], as.POSIXct(paste (ct3 [,1:2]), format = %MM/%DD/% %H:%M:%:S) I would appreciate any help in converting this data frame into xts. I am not sure is the NROW issue is the reason behind the failure or is it the data formate thanks - Original Message - From: R. Michael Weylandt michael.weyla...@gmail.com michael.weyla...@gmail.com To: Muhammad Abuizzah izzah...@yahoo.com Cc: r-help@R-project.org r-help@r-project.org Sent: Thursday, October 27, 2011 3:23 PM Subject: Re: [R] NROW doesn't equal length(x) Data frame is list internally so length(df) = ncol(df) M On Oct 27, 2011, at 2:44 PM, Muhammad Abuizzah izzah...@yahoo.com wrote: Hi, I am converting a data.frame to xts. the data.frame is 4 columns and 1000 rows. I get a message that NROW (x) must match length(order.by) class is data.frame, mode is list when I run dim(x) # I get 1000 4 #which is consistent with 1000 rows and 4 columns NROW (x) # I get 1000 # which is the right answer When I run length on each of columns in x separately using the $ I get 1000, which is the right number too. So length on each of the columns individually gives me the right answer, but length on the data.frame gives me the number of columns instead of the number of rows, is there an explanation thanks __ 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. __ 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] NROW doesn't equal length(x)
And to further the example, length() of matrix is not equal to the number of rows either. mm - matrix(1:6, ncol=2) length(mm) [1] 6 dim(mm) [1] 3 2 Also, NROW() and nrow() are different; I'd be cautious about using NROW without making sure I understood the difference. NROW function (x) if (is.array(x) || is.data.frame(x)) nrow(x) else length(x) environment: namespace:base nrow function (x) dim(x)[1L] environment: namespace:base -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 10/27/11 12:23 PM, R. Michael Weylandt michael.weyla...@gmail.com michael.weyla...@gmail.com wrote: Data frame is list internally so length(df) = ncol(df) M On Oct 27, 2011, at 2:44 PM, Muhammad Abuizzah izzah...@yahoo.com wrote: Hi, I am converting a data.frame to xts. the data.frame is 4 columns and 1000 rows. I get a message that NROW (x) must match length(order.by) class is data.frame, mode is list when I run dim(x) # I get 1000 4 #which is consistent with 1000 rows and 4 columns NROW (x) # I get 1000 # which is the right answer When I run length on each of columns in x separately using the $ I get 1000, which is the right number too. So length on each of the columns individually gives me the right answer, but length on the data.frame gives me the number of columns instead of the number of rows, is there an explanation thanks __ 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. __ 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. __ 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] NROW doesn't equal length(x)
Data frame is list internally so length(df) = ncol(df) M On Oct 27, 2011, at 2:44 PM, Muhammad Abuizzah izzah...@yahoo.com wrote: Hi, I am converting a data.frame to xts. the data.frame is 4 columns and 1000 rows. I get a message that NROW (x) must match length(order.by) class is data.frame, mode is list when I run dim(x) # I get 1000 4 #which is consistent with 1000 rows and 4 columns NROW (x) # I get 1000 # which is the right answer When I run length on each of columns in x separately using the $ I get 1000, which is the right number too. So length on each of the columns individually gives me the right answer, but length on the data.frame gives me the number of columns instead of the number of rows, is there an explanation thanks __ 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. __ 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] nrow()
Sandra, Please provide a small, reproducible example of this issue. You probably want to use ?is.nan and not the inequality operator. Similar example, contrast: x - NA is.na(x) x == NA Sandra Stankowski wrote: Hey there, I tried to count the number of rows, where my data isn't NaN in a certain column. this was my guess: (given is a data frame with 2069 rows and 17 cols) NROW(data[jan,16] != NaN) (jan is defined this way: jan - which(data[,2]==1, arr.ind= TRUE)) but I only get the number of columns where my data is 1 in the second col. R isn't removing the NaN. na.rm isn't working here. I would appreciate your help. __ 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. __ 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] nrow()
?is.nan Peter Ehlers On 2011-02-22 07:11, Sandra Stankowski wrote: Hey there, I tried to count the number of rows, where my data isn't NaN in a certain column. this was my guess: (given is a data frame with 2069 rows and 17 cols) NROW(data[jan,16] != NaN) (jan is defined this way: jan- which(data[,2]==1, arr.ind= TRUE)) but I only get the number of columns where my data is 1 in the second col. R isn't removing the NaN. na.rm isn't working here. I would appreciate your help. __ 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. __ 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] nrow()
Hi Sandra, What about ?is.na function ? Hope this help Regards, ML Le 22/02/11 16:11, Sandra Stankowski a écrit : NROW(data[jan,16] != NaN) -- Mohamed Lajnef,IE INSERM U955 eq 15# Pôle de Psychiatrie# Hôpital CHENEVIER # 40, rue Mesly # 94010 CRETEIL Cedex FRANCE # mohamed.laj...@inserm.fr # tel : 01 49 81 31 31 (poste 18467) # Sec : 01 49 81 32 90 # fax : 01 49 81 30 99 # [[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] nrow()
Sandra Stankowski wrote: is.na function does'nt seem to work, but maybe I'm just dealing with it in a wrong way. here's an example m - c(2, 3, 5, 6, 3, 7, -99, -99, 6) n - c(1,1,1,1,1,2,2,2,2) so my matrix contains certain missing values Thank you for the example. You're constructing a data.frame, not a matrix. Those are two separate classes in R. m[m==-99] - NA o - data.frame(m, n) o m n 1 2 1 2 3 1 3 5 1 4 6 1 5 3 1 6 7 2 7 NA 2 8 NA 2 9 6 2 2 stands for february february - which(o[,2]==2, arr.ind = TRUE) prec_feb - sum(o[february,1], na.rm = TRUE) prec_feb [1] 13 And now I need to know the exact number of rows, where m contains a value. to know how many days a month give any information. (to create monthly means and stuff) You might find ?complete.cases useful. Also try: sum(!is.na(o$m)) ?tapply may also be useful in general: e.g., tapply(o$m, o$n, mean, na.rm = TRUE) None of this is tested... hope this explains, what I need to know. Thanks, S. Am 22.02.2011 16:50, schrieb Erik Iverson: Sandra, Please provide a small, reproducible example of this issue. You probably want to use ?is.nan and not the inequality operator. Similar example, contrast: x - NA is.na(x) x == NA Sandra Stankowski wrote: Hey there, I tried to count the number of rows, where my data isn't NaN in a certain column. this was my guess: (given is a data frame with 2069 rows and 17 cols) NROW(data[jan,16] != NaN) (jan is defined this way: jan - which(data[,2]==1, arr.ind= TRUE)) but I only get the number of columns where my data is 1 in the second col. R isn't removing the NaN. na.rm isn't working here. I would appreciate your help. __ 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. __ 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] nrow()
is.na function does'nt seem to work, but maybe I'm just dealing with it in a wrong way. here's an example m - c(2, 3, 5, 6, 3, 7, -99, -99, 6) n - c(1,1,1,1,1,2,2,2,2) so my matrix contains certain missing values m[m==-99] - NA o - data.frame(m, n) o m n 1 2 1 2 3 1 3 5 1 4 6 1 5 3 1 6 7 2 7 NA 2 8 NA 2 9 6 2 2 stands for february february - which(o[,2]==2, arr.ind = TRUE) prec_feb - sum(o[february,1], na.rm = TRUE) prec_feb [1] 13 And now I need to know the exact number of rows, where m contains a value. to know how many days a month give any information. (to create monthly means and stuff) hope this explains, what I need to know. Thanks, S. Am 22.02.2011 16:50, schrieb Erik Iverson: Sandra, Please provide a small, reproducible example of this issue. You probably want to use ?is.nan and not the inequality operator. Similar example, contrast: x - NA is.na(x) x == NA Sandra Stankowski wrote: Hey there, I tried to count the number of rows, where my data isn't NaN in a certain column. this was my guess: (given is a data frame with 2069 rows and 17 cols) NROW(data[jan,16] != NaN) (jan is defined this way: jan - which(data[,2]==1, arr.ind= TRUE)) but I only get the number of columns where my data is 1 in the second col. R isn't removing the NaN. na.rm isn't working here. I would appreciate your help. __ 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. __ 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] nrow()
Thanks. I made it! Best wishes, S. Am 22.02.2011 17:41, schrieb Erik Iverson: Sandra Stankowski wrote: is.na function does'nt seem to work, but maybe I'm just dealing with it in a wrong way. here's an example m - c(2, 3, 5, 6, 3, 7, -99, -99, 6) n - c(1,1,1,1,1,2,2,2,2) so my matrix contains certain missing values Thank you for the example. You're constructing a data.frame, not a matrix. Those are two separate classes in R. m[m==-99] - NA o - data.frame(m, n) o m n 1 2 1 2 3 1 3 5 1 4 6 1 5 3 1 6 7 2 7 NA 2 8 NA 2 9 6 2 2 stands for february february - which(o[,2]==2, arr.ind = TRUE) prec_feb - sum(o[february,1], na.rm = TRUE) prec_feb [1] 13 And now I need to know the exact number of rows, where m contains a value. to know how many days a month give any information. (to create monthly means and stuff) You might find ?complete.cases useful. Also try: sum(!is.na(o$m)) ?tapply may also be useful in general: e.g., tapply(o$m, o$n, mean, na.rm = TRUE) None of this is tested... hope this explains, what I need to know. Thanks, S. Am 22.02.2011 16:50, schrieb Erik Iverson: Sandra, Please provide a small, reproducible example of this issue. You probably want to use ?is.nan and not the inequality operator. Similar example, contrast: x - NA is.na(x) x == NA Sandra Stankowski wrote: Hey there, I tried to count the number of rows, where my data isn't NaN in a certain column. this was my guess: (given is a data frame with 2069 rows and 17 cols) NROW(data[jan,16] != NaN) (jan is defined this way: jan - which(data[,2]==1, arr.ind= TRUE)) but I only get the number of columns where my data is 1 in the second col. R isn't removing the NaN. na.rm isn't working here. I would appreciate your help. __ 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. __ 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.