Re: [R] if/else help
Thanks very much for your detailed reply to my post. Very helpful/useful tool(s) you’ve provide me. Best wishes, B. From: William Dunlap [mailto:wdun...@tibco.com] Sent: Wednesday, September 21, 2016 10:48 AM To: Crombie, Burnette N <bcrom...@utk.edu> Cc: r-help@r-project.org Subject: Re: [R] if/else help If you write your code as functions you can avoid the nasty 'if(exists("x"))x<-...' business this by writing default values for arguments to your function. They will be computed only when they are used. E.g., analyzeData <- function(a=0, b=0, c=0, d="x", r4 = data.frame(a, b, c, d)) { summary(r4) } > analyzeData(c=101:102) a b c d Min. :0 Min. :0 Min. :101.0 x:2 1st Qu.:0 1st Qu.:0 1st Qu.:101.2 Median :0 Median :0 Median :101.5 Mean :0 Mean :0 Mean :101.5 3rd Qu.:0 3rd Qu.:0 3rd Qu.:101.8 Max. :0 Max. :0 Max. :102.0 > analyzeData(r4=data.frame(a=10:11,b=20:21,c=30:31,d=c("x","y"))) a b c d Min. :10.00 Min. :20.00 Min. :30.00 x:1 1st Qu.:10.25 1st Qu.:20.25 1st Qu.:30.25 y:1 Median :10.50 Median :20.50 Median :30.50 Mean :10.50 Mean :20.50 Mean :30.50 3rd Qu.:10.75 3rd Qu.:20.75 3rd Qu.:30.75 Max. :11.00 Max. :21.00 Max. :31.00 Bill Dunlap TIBCO Software wdunlap tibco.com<http://tibco.com> On Tue, Sep 20, 2016 at 12:31 PM, Crombie, Burnette N <bcrom...@utk.edu<mailto:bcrom...@utk.edu>> wrote: If a data.frame (r4) does not exist in my R environment, I would like to create it before I move on to the next step in my script. How do I make that happen? Here is what I want to do from a code perspective: if (exists(r4)) { is.data.frame(get(r4)) } else { a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) } Thanks for your help, B [[alternative HTML version deleted]] __ R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To UNSUBSCRIBE and more, see 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 -- To UNSUBSCRIBE and more, see 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] if/else help
Thank you for your time, Don. Exactly what I was looking for - a one-liner. Feedback from others on this post has been good to expand my knowledge, though. I'm too old for homework but have just started using R if/else, loops, and functions and trying to get the hang of them. Best wishes - B -Original Message- From: MacQueen, Don [mailto:macque...@llnl.gov] Sent: Wednesday, September 21, 2016 11:26 AM To: Crombie, Burnette N <bcrom...@utk.edu>; r-help@r-project.org Subject: Re: [R] if/else help Hopefully this is not a homework question. The other responses are fine, but I would suggest the simplest way to do exactly what you ask is if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x') The exists() function requires a character string for its first argument, i.e., the name of the object, not the object itself (check the help page for exists). Using "get" to get it doesn't make sense if it already exists. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N" <r-help-boun...@r-project.org on behalf of bcrom...@utk.edu> wrote: >If a data.frame (r4) does not exist in my R environment, I would like >to create it before I move on to the next step in my script. How do I >make that happen? Here is what I want to do from a code perspective: > >if (exists(r4)) >{ >is.data.frame(get(r4)) >} >else >{ >a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) } > >Thanks for your help, >B > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >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 -- To UNSUBSCRIBE and more, see 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] if/else help
If a data.frame (r4) does not exist in my R environment, I would like to create it before I move on to the next step in my script. How do I make that happen? Here is what I want to do from a code perspective: if (exists(r4)) { is.data.frame(get(r4)) } else { a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) } Thanks for your help, B [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to round only one df row how to keep 3rd sigdif if zero
Thanks for taking the time to share your thoughts, PP. I always extensively google search before resorting to R forum. In my real dataset, not in the example I created for the forum, I had tried converting the matrix to a dataframe but it retained the unwanted format. And, these tables are being used in a report generated with the rtf package, so I have to get the format right for outside the console. Because of another unrelated issue, though, I had to use a different approach to creating the dataframe with counts/rates added, so the issue was circumvented. Cheers. -Original Message- From: PIKAL Petr [mailto:petr.pi...@precheza.cz] Sent: Thursday, June 18, 2015 10:56 AM To: Crombie, Burnette N; r-help@r-project.org Subject: RE: [R] How to round only one df row how to keep 3rd sigdif if zero Hi You need to distinguish between an object and printing an object on console. When you print an object you can use several options for formating. ?sprintf, ?formatC formatC(t(a), digits=1, format=f) [,1] [,2] [,3] count 1.0 2.0 3.0 rate 16.7 33.3 50.0 Also when you transpose a the result is not data frame but matrix. str(t(a)) num [1:2, 1:3] 1 16.7 2 33.3 3 50 - attr(*, dimnames)=List of 2 ..$ : chr [1:2] count rate ..$ : NULL str(a) 'data.frame': 3 obs. of 2 variables: $ count: num 1 2 3 $ rate : num 16.7 33.3 50 If you used google or other internet search options you would get plenty of results yourself. try formatting numbers R Cheers Petr -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of bcrombie Sent: Thursday, June 18, 2015 3:09 PM To: r-help@r-project.org Subject: [R] How to round only one df row how to keep 3rd sigdif if zero # How do I round only one row of a dataframe? # After transposing a dataframe of counts rates, all values took on the most # of signif digits in the dataset (rates), but I want counts to remain only one digit. # Also, how can I keep 3 significant digits in R when the 3rd is a zero? count - c(1, 2, 3) rate - c(16.7, 33.3, 50.0) a - data.frame(count,rate) a # count rate # 1 1 16.7 # 2 2 33.3 # 3 3 50.0 a - t(a) a # [,1] [,2] [,3] # count 1.0 2.03 # rate 16.7 33.3 50 -- View this message in context: http://r.789695.n4.nabble.com/How-to- round-only-one-df-row-how-to-keep-3rd-sigdif-if-zero-tp4708819.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému. Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu. V případě, že je tento e-mail součástí obchodního jednání: - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou. - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech. - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains
Re: [R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display
That is the solution I had tried first (yes, it's nice!), but it doesn't provide the other PViol.Type's that aren't necessarily in my dataset. That's where my problem is. I'm closer to the cure, though, and think I've thought of a solution as soon as I have time. I'll update everyone then. -- BNC -Original Message- From: John Kane [mailto:jrkrid...@inbox.com] Sent: Friday, December 19, 2014 8:44 AM To: Sven E. Templer; Chel Hee Lee Cc: R Help List; Crombie, Burnette N Subject: Re: [R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display Very pretty. I could have saved myself about 1/2 hour of mucking about if I had thought ot length. John Kane Kingston ON Canada -Original Message- From: sven.temp...@gmail.com Sent: Fri, 19 Dec 2014 10:13:55 +0100 To: chl...@mail.usask.ca Subject: Re: [R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display Another solution: CaseID - c(1015285, 1005317, 1012281, 1015285, 1015285, 1007183, 1008833, 1015315, 1015322, 1015285) Primary.Viol.Type - c(AS.Age, HS.Hours, HS.Hours, HS.Hours, RK.Records_CL, OT.Overtime, OT.Overtime, OT.Overtime, V.Poster_Other, V.Poster_Other) library(reshape2) dcast(data.frame(CaseID, Primary.Viol.Type), CaseID~Primary.Viol.Type, length) # result: Using Primary.Viol.Type as value column: use value.var to override. CaseID AS.Age HS.Hours OT.Overtime RK.Records_CL V.Poster_Other 1 1005317 01 0 0 0 2 1007183 00 1 0 0 3 1008833 00 1 0 0 4 1012281 01 0 0 0 5 1015285 11 0 1 1 6 1015315 00 1 0 0 7 1015322 00 0 0 1 best, s. On 19 December 2014 at 06:35, Chel Hee Lee chl...@mail.usask.ca wrote: Please take a look at my code again. The error message says that object 'Primary.Viol.Type' not found. Have you ever created the object 'Primary.Viol.Type'? It will be working if you replace 'Primary.Viol.Type' by 'PViol.Type.Per.Case.Original$Primary.Viol.Type' where 'factor()' is used. I hope this helps. Chel Hee Lee On 12/18/2014 08:57 PM, Crombie, Burnette N wrote: Chel, your solution is fantastic on the dataset I submitted in my question but it is not working when I import my real dataset into R. Do I need to vectorize the columns in my real dataset after importing? I tried a few things (###) but not making progress: MERGE_PViol.Detail.Per.Case - read.csv(~/FOIA_FLSA/MERGE_PViol.Detail.Per.Case_for_rtf10.csv, stringsAsFactors=TRUE) ### select only certain columns PViol.Type.Per.Case.Original - MERGE_PViol.Detail.Per.Case[,c(CaseID, Primary.Viol.Type)] ### write.csv(PViol.Type.Per.Case,file=PViol.Type.Per.Case.Select.csv) ### PViol.Type.Per.Case.Original - read.csv(~/FOIA_FLSA/PViol.Type.Per.Case.Select.csv) ### PViol.Type.Per.Case.Original$X - NULL ###PViol.Type.Per.Case.Original[] - lapply(PViol.Type.Per.Case.Original, as.character) PViol.Type - c(CaseID, BW.BackWages, LD.Liquid_Damages, MW.Minimum_Wage, OT.Overtime, RK.Records_FLSA, V.Poster_Other, AS.Age, BW.WHMIS_BackWages, HS.Hours, OA.HazOccupationAg, ON.HazOccupationNonAg, R3.Reg3AgeOccupation, RK.Records_CL, V.Other) PViol.Type.Per.Case.Original$Primary.Viol.Type - factor(Primary.Viol.Type, levels=PViol.Type, labels=PViol.Type) ### Error in factor(Primary.Viol.Type, levels = PViol.Type, labels = PViol.Type) : object 'Primary.Viol.Type' not found tmp - split(PViol.Type.Per.Case.Original,PViol.Type.Per.Case.Original$Case ID) ans - ifelse(do.call(rbind, lapply(tmp, function(x)table(x$Primary.Viol.Type))), 1, NA) -Original Message- From: Crombie, Burnette N Sent: Thursday, December 18, 2014 3:01 PM To: 'Chel Hee Lee' Subject: RE: [R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display Thanks for taking the time to review this, Chel. I've got to step away from my desk, but will reply more substantially as soon as possible. -- BNC -Original Message- From: Chel Hee Lee [mailto:chl...@mail.usask.ca] Sent: Thursday, December 18, 2014 2:43 PM To: Jeff Newmiller; Crombie, Burnette N Cc: r-help@r-project.org Subject: Re: [R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display I like the approach presented by Jeff Newmiller as shown in the previous post (I really like his way). As he suggested, it would be good
Re: [R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display
I want to achieve a table that looks like a grid of 1's for all cases in a survey. I'm an R beginner and don't have a clue how to do all the things you just suggested. I really appreciate the time you took to explain all of those options, though. -- BNC -Original Message- From: Boris Steipe [mailto:boris.ste...@utoronto.ca] Sent: Thursday, December 18, 2014 5:29 AM To: Crombie, Burnette N Cc: r-help@r-project.org Subject: Re: [R] Make 2nd col of 2-col df into header row of same df then adjust col1 data display What you are describing sounds like a very spreadsheet-y thing. - The information is already IN your dataframe, and easy to get out by subsetting. Depending on your usecase, that may actually be the best. - If the number of CaseIDs is large, I would use a hash of lists (if the data is sparse), or hash of named vectors if it's not sparse. Lookup is O(1) so that may be the best. (Cf package hash, and explanations there). - If it must be the spreadsheet-y thing, you could make a matrix with rownames and colnames taken from unique() of your respective dataframe. Instead of 1 and NA I probably would use TRUE/FALSE. - If it takes less time to wait for the results than to look up how apply() works, you can write a simple loop to populate your matrix. Otherwise apply() is much faster. - You could even use a loop to build the datastructure, checking for every cbind() whether the value in column 1 already exists in the table - but that's terrible and would make a kitten die somewhere on every iteration. All of these are possible, and you haven't told us enough about what you want to achieve to figure out what the best is. If you choose one of the options and need help with the code, let us know. Cheers, B. On Dec 17, 2014, at 10:15 PM, bcrombie bcrom...@utk.edu wrote: # I have a dataframe that contains 2 columns: CaseID - c('1015285', '1005317', '1012281', '1015285', '1015285', '1007183', '1008833', '1015315', '1015322', '1015285') Primary.Viol.Type - c('AS.Age', 'HS.Hours', 'HS.Hours', 'HS.Hours', 'RK.Records_CL', 'OT.Overtime', 'OT.Overtime', 'OT.Overtime', 'V.Poster_Other', 'V.Poster_Other') PViol.Type.Per.Case.Original - data.frame(CaseID,Primary.Viol.Type) # CaseID's can be repeated because there can be up to 14 Primary.Viol.Type's per CaseID. # I want to transform this dataframe into one that has 15 columns, where the first column is CaseID, and the rest are the 14 primary viol. types. The CaseID column will contain a list of the unique CaseID's (no replicates) and for each of their rows, there will be a 1 under a column corresponding to a primary violation type recorded for that CaseID. So, technically, there could be zero to 14 1's in a CaseID's row. # For example, the row for CaseID '1015285' above would have a 1 under AS.Age, HS.Hours, RK.Records_CL, and V.Poster_Other, but have NA under the rest of the columns. PViol.Type - c(CaseID, BW.BackWages, LD.Liquid_Damages, MW.Minimum_Wage, OT.Overtime, RK.Records_FLSA, V.Poster_Other, AS.Age, BW.WHMIS_BackWages, HS.Hours, OA.HazOccupationAg, ON.HazOccupationNonAg, R3.Reg3AgeOccupation, RK.Records_CL, V.Other) PViol.Type.Columns - t(data.frame(PViol.Type) # What is the best way to do this in R? -- View this message in context: http://r.789695.n4.nabble.com/Make-2nd-col-of-2-col-df-into-header-row -of-same-df-then-adjust-col1-data-display-tp4700878.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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 -- To UNSUBSCRIBE and more, see 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] count and sum simultaneously in R pivot table
A.K., thanks for your reply. I'm getting an error at res2: Error: unexpected symbol in res2 - within(as.data.frame(res1),`Count of Case ID` - dcast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, length, margins=TRUE)[,3])[,c(4,1:3)] colnames I've tried a couple of modifications, but obviously don't know what I'm doing because I haven't fixed it. If anything comes to you in the meantime, please advise. Thanks. -Original Message- From: arun [mailto:smartpink...@yahoo.com] Sent: Tuesday, February 18, 2014 2:28 AM To: r-help@r-project.org Cc: Crombie, Burnette N Subject: Re: [R] count and sum simultaneously in R pivot table Hi, Check if this works: library(reshape2) res1 - acast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, sum, margins=TRUE)[,-4] res2 - within(as.data.frame(res1),`Count of Case ID` - dcast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, length, margins=TRUE)[,3])[,c(4,1:3)] colnames(res2)[2:4] - paste(Sum of,colnames(res2)[2:4]) rownames(res2)[length(rownames(res2))] - Grand Total indx - grepl(all,rownames(res2)) ord1 - unlist(tapply(seq_len(nrow(res2)),list(cumsum(c(TRUE,diff(indx)0))),FUN=function(x) c(tail(x,1),head(x,-1)) ),use.names=FALSE) res3 - res2[ord1,] rownames(res3) - gsub(\\_\\(all\\),,rownames(res3)) A.K. __ 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] count and sum simultaneously in R pivot table
The script works nicely, Arun. You were right, I pasted code from email instead of Rhelp and didn't reformat properly in R. I appreciate your time with this! -Original Message- It seems like part of the next line is also being run (in the end 'colnames'). For e.g. res2 - within(as.data.frame(res1),`Count of Case ID` - dcast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, length, margins=TRUE)[,3])[,c(4,1:3)] colnames #Error: unexpected symbol in res2 - within(as.data.frame(res1),`Count of Case ID` - #dcast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, length, margins=TRUE)#[,3])[,c(4,1:3)] colnames __ 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] condense repetitive code for read.csv and rename.vars
Thanks very much for your contribution, Siraaj. I appreciate you taking the time to help me learn loops, etc. BNC -Original Message- From: Siraaj Khandkar [mailto:sir...@khandkar.net] Sent: Wednesday, August 14, 2013 9:08 PM To: Crombie, Burnette N Cc: r-help@r-project.org Subject: Re: [R] condense repetitive code for read.csv and rename.vars On 08/14/2013 03:43 PM, bcrombie wrote: Is there a more concise way to write the following code? library(gdata) mydataOUTPUTrtfA - read.csv(mergedStatstA.csv) save(mydataOUTPUTrtfA, file=mydataOUTPUTrtfA.RData) mydataOUTPUTrtfA - rename.vars(mydataOUTPUTrtfA, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfB - read.csv(mergedStatstB.csv) save(mydataOUTPUTrtfB, file=mydataOUTPUTrtfB.RData) mydataOUTPUTrtfB - rename.vars(mydataOUTPUTrtfB, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfC - read.csv(mergedStatstC.csv) save(mydataOUTPUTrtfC, file=mydataOUTPUTrtfC.RData) mydataOUTPUTrtfC - rename.vars(mydataOUTPUTrtfC, from=X, to=Statistics.Calculated, info=FALSE) I will have a series of mydataOUTPUTrtf files spanning a large portion of the alphabet, so to speak: e.g. mydataOUTPUTrtfA to mydataOUTPUTrtfG --- thanks for your help alphabet - c(FOO, BAR, BAZ) for (a in alphabet) { filename - paste(c(basename, a, .csv), collapse=) data - read.csv(filename) date - rename.vars( data , from=X , to=Statistics.Calculated , info=FALSE ) # do some other stuff with data } You should be able to pick it up from here. In case you need an actual alphabet, it is already predefined: LETTERS [1] A B C D E F G H I J K L M N O P Q [18] R S T U V W X Y Z letters [1] a b c d e f g h i j k l m n o p q [18] r s t u v w x y z __ 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] create new matrix from user-defined function
Oh, also thanks for the speed comparisons. Missed that in my first read-through. Very interesting and informative. BNC -Original Message- From: Crombie, Burnette N Sent: Thursday, July 11, 2013 4:40 PM To: 'arun' Cc: R help Subject: RE: [R] create new matrix from user-defined function You understood me perfectly, and I agree is it easier to index using numbers than names. I'm just afraid if my dataset gets too big I'll mess up which index numbers I'm supposed to be using. data.table() looks very useful and a good way to approach the issue. Thanks. I really appreciate your (everyone's) help. BNC __ 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] help with text patterns in strings
Thanks, Arun. I will study this as soon as possible. I really appreciate your time and R mentoring. Try this: res1-sapply(vec3,function(x) length(vec2New[grep(x,vec2New)]) ) dat1-data.frame(res1,Name=names(vec3)) dat1$Name-factor(dat1$Name,levels=c(early,mid,late,wknd)) with(dat1,tapply(res1,list(Name),FUN=sum)) #early mid late wknd # 0 1 4 6 #or sapply(split(res1,names(vec3)),sum) #early late mid wknd # 0 4 1 6 A.K. __ 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.