Re: [R] subsetting data by specified observation number
What is wrong with subset( failed.3, position == 2 ) ? -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. Linh Tran wrote: Hi members, I'd like to thank you guys ahead of time for the help. I'm kind of stuck. I have a data frame with ID and position numbers: 1> head(failed.3) id position 1 1997 2 4 1000RW_M 2 15 1006RW_G 2 24 1012RW_M 3 28 10160917 2 30 1016RW_M 13 I'd like to use this to subset out a large dataset and keep only the observation number corresponding to the position number. So for example, ID 1997 has 10 observations. I want to keep the 2nd one only. Thanks, -linh_ 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] Date arithmetic coerces POSIXlt to POSIXct?
You should not be surprised at any result you obtain by adding an integer to a POSIXlt... that is like adding 5 to 3 meters... 5 whats? Start by using as.difftime to specify your time units on time intervals. As it happens, the default unit used by POSIXt types is seconds, and POSIXlt is not designed to support arithmetic, so a conversion to POSIXct is performed automatically. Don't forget that POSIXt handles time zones, including daylight savings, but data rarely includes the timezone in it, so you ought to configure the TZ environment variable before working with the data you have in a POSIXt variable. Many people find it easier to use Date or chron types for simple tasks. In any case, it is documented, and it is not a bug. -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. "Buttrey, Samuel (Sam) (CIV)" wrote: Hi. This feels like a bug to me, or at least an undocumented feature, but I thought I'd see what people here thought of it. Consider a POSIXlt object like this one: > a <- as.POSIXlt ("2011-01-23 12:45:45") > class (a) [1] "POSIXlt" "POSIXt" Fine. Now, if I do some arithmetic on that object, the result is converted to POSIXct. > class (a - 360) [1] "POSIXct" "POSIXt" Does this seem weird? I'm running R 2.12.1 under Windows XP. Thanks, Sam Buttrey [[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. [[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] Reshape, melt and cast query
I can't see the value of your first melt, which appears to result in a column filled with identical values "crop_group". You really should read the posting guide and provide a reproducible example. I suspect that you would benefit from learning how to use the plyr library. -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. Muzna Alvi wrote: thanks for that ista actually i want to break this into maybe three different data sets with each of the 3 kinds of sugarcane... it would be better if i could have this in one data set but if someone could tell me how i can run descriplitive statistics on each of the groups separately.. i would need to melt and cast for that right? On Thu, Mar 10, 2011 at 8:46 PM, Ista Zahn wrote: > Hi Muzna, > If I understand your question, it's not really about melting or > casting, but about re-ordering. Try this: > > canefile <- canefile[order(canefile$value), ] > > see ?"[" and ?order > > HTH, > Ista > > On Thu, Mar 10, 2011 at 2:20 PM, Muzna Alvi wrote: > > I have a dataset that is based on crop output for a single crop > > *sugarcane*...but > > each observation is further subdivided into 3 kinds of sugarcane > > i have read the file and have also used melt it to sort it on the > > basis of *crop > > group* using > > > > melt(sugarcane, m=c("Crop_group")) -> canefile > > > > this is how it has cast the data > > > > variable value > > 1 Crop_group Sugarcane (first ratoon) > > 2 Crop_group Sugarcane (planted crop) > > 3 Crop_group Sugarcane (planted crop) > > 4 Crop_group Sugarcane (second ratoon) > > 5 Crop_group Sugarcane (first ratoon) > > 6 Crop_group Sugarcane (planted crop) > > > > > > now i need to *cast *the data so that i can have each of the 20 odd > > variables sorted according to the 3 subdivisions under *Crop_group *ie > first > > ratoon), second ratoon etc etc.. > > > > i tried using > > > > cast (canefile, ~value) -> canefile > > > > but it said "Aggregation requires fun.aggregate: length used as default" > > > > i want to cast it on all the 20 variables and sort it on the basis of > first > > ratoon, second ratoon and planted crop > > > > How i do my CAST command? > > > > I hope my question is clear...thanks in advance > > > > * > > * > > * > > * > > -- > > -- > > > > [[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. > > > > > > -- > > > Ista Zahn > Graduate student > University of Rochester > Department of > > Clinical and Social Psychology > http://yourpsyche.org > -- -- > > [[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. [[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] Timezone issue with strftime/strptime and %z and %Z
I have found the various "tz" arguments not to provide the support I would like in terms of arbitrary timezones. For now I use Sys.setenv(TZ="timezone_spec") prior to input or output, and do not use the tz arguments. In your case, I would consider keeping a character copy of the input to write out later. -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. "Daniel Nüst" wrote: 2011/3/11 David Winsemius : > On Mar 10, 2011, at 8:46 PM, David Winsemius wrote: >> On Mar 10, 2011, at 11:17 AM, Daniel Nüst wrote: >>> I try to parse a time stamp with time zone. I essentially just want to >>> parse the time stamp "1995-05-25T15:30:00+10:00" and output it exactly >>> like it is, using the POSIX classes (or is that impossible?). >> >> Does this work? >> >> as.POSIXlt(gsub("T.*(\\+|\\-)..(:)", "", # get rid of the colon in the tz >> # but preserve the sign for the %z format string >> gsub("T", " ", "1995-05-25T15:30:00-1000")), # replace the "T" >> with a space >> format="%Y-%m-%d %H:%M:%S%z") > > Daniel; > > That example didn't have the full complexity of the question so I didn't > notoce that I had incorrectly constructed a regex OR within the colon > handling clause. I neverdid figure out how to do that properly, but htis > should handle it: > > as.POSIXlt(gsub("T", " ", #change T to space > + # but preserve the sign for the %z format string > + gsub("(T..:..:.):", "\\1", "1995-05-25T15:30:00-10:00")), > format="%Y-%m-%d %H:%M:%S%z") > [1] "1995-05-25 21:30:00" > > To get output in GMT add tz argument to as.POSIXlt: > >> as.POSIXlt(gsub("T", " ", #change T to space > + # but preserve the sign for the %z format string > + gsub("(T..:..:.):", "\\1", "1995-05-25T15:30:00-10:00")), > format="%Y-%m-%d %H:%M:%S%z", tz="GMT") > [1] "1995-05-26 01:30:00 GMT" This procudes the same output on my machine, but still does not solve the problem, which lies on the output side, sorry if I was not clear about that before: time <- as.POSIXlt(gsub("T", " ", # change T to space # but preserve the sign for the %z format string gsub("(T..:..:.):", "\\1", "1995-05-25T15:30:00-10:00")), format="%Y-%m-%d %H:%M:%S%z") #, tz="GMT") format(x = time, format = "%Y-%m-%d %H:%M:%S%z") # [1] "1995-05-26 01:30:00Mitteleuropäische Zeit" strftime(x = time, format = "%Y-%m-%d %H:%M:%S%z") # [1] "1995-05-26 03:30:00Mitteleuropäische Sommerzeit" But I need "...01:30:00+01:00", or "...01:30:00+0100", as I expect from %z because in ?strptime I read: "%z Signed offset in hours and minutes from UTC, so -0800 is 8 hours behind UTC." and "Note that when %z or %Z is used for output with an object with an assigned timezone an attempt is made to use the values for that timezone â but it is not guaranteed to succeed." Does this just mean it just won't work? Let me rephrase my question: How can I create a time object from the character string "1995-05-25T15:30:00-10:00" and get exactly the same character string again when formatting it/printing it? Thanks for any help! /Daniel >>> sessionInfo() >>> >>> R version 2.12.1 (2010-12-16) >>> Platform: x86_64-pc-mingw32/x64 (64-bit) >>> >>> locale: >>> [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 >>> [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C >>> [5] LC_TIME=German_Germany.1252 >>> >>> attached base packages: >>> [1] stats graphics grDevices utils datasets methods base >>> >>> other attached packages: >>> [1] rj_0.5.2-1 >>> >>> loaded via a namespace (and not attached): >>> [1] rJava_0.8-8 tools_2.12.1 t1 <- strptime("1995-05-25T15:30:00+10:00", format = "%Y-%m-%dT%H:%M:%OS") t2 <- strptime("1995-05-25T15:30:00+10:00", format = "%Y-%m-%dT%H:%M:%OS%z") strftime(t1, format = "%Y-%m-%dT%H:%M:%OS") >>> >>> [1] "1995-05-25T15:30:00" strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%z") >>> >>> [1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit" # Ends in "Mitteleuropäische Sommerzeit", not in +10:00, so time zone is ignored! # Also no difference beetween %z and %z ! strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%Z") >>> >>> [1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit" # All this does NOT remove the "Mitteleuropäische Zeit" from the > >>> strftime output!! # Can locale solve the problem? Sys.getlocale(category = "LC_TIME") >>> >>> [1] "German_Germany.1252" Sys.setlocale("LC_TIME", "English") >>> >>> [1] "English_United States.1252" strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%z") >>> >>> [1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit" # [1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit" -- No change. # does t1 actually have time zone? attributes(t1) >>> >>> $names >>> [1] "sec" "min" "h