[R] Select a set of rows based on a condition
Hi, I need help with selecting a set of rows from a column in a dataset, that matches a string criteria - start and end. The dataset is : variable Name Value List|Index 10 ABC 20 DEF 10 GHI 50 JKL40 MNO 20 PQR 10 Start=DEF End = MNO Ideally, I would like to select the data from DEF to MNO into a new data set. My logic is 1) get the row number for DEF 2) get the row number for MNO 3) subset the rows from DEF to MNO Can someone please help me with the R code for this as am a little rusty. Please let me know if you have questions. Thanks in advance! Raoul -- View this message in context: http://r.789695.n4.nabble.com/Select-a-set-of-rows-based-on-a-condition-tp4168594p4168594.html Sent from the R help mailing list archive at Nabble.com. __ 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] Select a set of rows based on a condition
Thanks a million Michael! Works perfectly! Really appreciate it! -- View this message in context: http://r.789695.n4.nabble.com/Select-a-set-of-rows-based-on-a-condition-tp4168594p4171746.html Sent from the R help mailing list archive at Nabble.com. __ 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] Coding a new variable based on criteria in a dataset
Hi, I'm a bit stuck and need some help with R code to code a variable F_R based on a combination of conditions. The first condition would code F_R as F and would be based on the min(Date) and Min(Time) for each combination of UniqueID Reason. The second condition would code the variable as R as it would be the rest of the data that dont meet the first condition. For example: for UID 1 Reason 1 the first record would be coded F and the 4th record would be coded R. UniqueID Reason Date Time 1 UID 1 Reason 1 19/12/2010 15:00 2 UID 1 Reason 2 19/12/2010 16:00 3 UID 1 Reason 3 19/12/2010 16:30 4 UID 1 Reason 1 20/12/2010 08:00 5 UID 1 Reason 2 20/12/2010 10:01 6 UID 1 Reason 3 20/12/2010 11:30 7 UID 1 Reason 1 21/12/2010 12:45 8 UID 1 Reason 2 21/12/2010 18:44 9 UID 1 Reason 3 21/12/2010 19:29 10UID 2 Reason 1 19/12/2010 17:00 11UID 2 Reason 2 19/12/2010 18:00 12UID 2 Reason 3 19/12/2010 18:10 13UID 2 Reason 1 20/12/2010 13:00 14UID 2 Reason 2 20/12/2010 13:30 15UID 2 Reason 3 20/12/2010 16:15 Is a loop the most efficient way to do this or is there some pre-existing function that can help me with this? The sample dataset is what is given below. Thanks in advance, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Coding-a-new-variable-based-on-criteria-in-a-dataset-tp3145176p3145176.html Sent from the R help mailing list archive at Nabble.com. __ 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] Problem with ggplot2 - Boxplot
Thanks a lot Hadley, this worked. Regards, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-ggplot2-Boxplot-tp2549970p2952914.html Sent from the R help mailing list archive at Nabble.com. __ 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 with panel.text in Lattice - Putting labels for co-oridnates in a plot
Hi, I am trying to create a Lattice dotplot that has the following data graphed. I need to put labels for each of the co-oridnates on the plot. I have managed to get only one label dispalyed as I don't completely understand the panel.text function. Can someone please help me? # Sub Reason is a text field that I need to see the volumes for (Vols) dotplot(DU_Summary_plotdata$SubReason ~ DU_Summary_plotdata$Vols ,horiz=TRUE,main=Top Sub-Reasons - Volumes (90% of Volumes), family=serif,font=2,xlab=Volumes,ylab=Sub-Reasons,labels=DU_Summary_plotdata$Vols,pch=,cex=1.5, panel = function(x, y, ...) { panel.dotplot(x, y, ...) panel.text(1,2,labels =DU_Summary_plotdata$Vols , pos = 4) }) The dataset DU_Summary_plotdata is made up of: SubReason-c( SR_1, SR_2 , SR_3, SR_4, SR_5, SR_6, SR_7, SR_8) Vols-c( 33827,17757,11404,5999,5305,3515,3051,1924) Thanks, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Help-with-panel-text-in-Lattice-Putting-labels-for-co-oridnates-in-a-plot-tp2952919p2952919.html Sent from the R help mailing list archive at Nabble.com. __ 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] Problem with ggplot2 - Boxplot
Hi, I am using ggplot2 to create a boxplot that summarizes a continuous variable. This code works fine for me on one PC however when I use it on another it doesnt. The structure of the dataset AHT_TopCD is SubReason=Categorical variable, AHT=Continuous variable. The code for the boxplot: require(ggplot2) qplot(SubReason,AHT,data=AHT_TopCD,geom=boxplot,main=AHT Spread - By Sub-Reason,xlab=AHT,colour=SubReason,alpha = I(1 / 5))+ + coord_flip() + scale_x_discrete(breaks=NA) The error I get is : Error in get(make_aesthetics, env = x, inherits = TRUE)(x, ...) : could not find function empty I do not understand this error. Can anyone help me with this please? Also, let me know if you have any questions or require clarification on anything here. Regards, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-ggplot2-Boxplot-tp2549970p2549970.html Sent from the R help mailing list archive at Nabble.com. __ 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] Data Labels in a barchart (Lattice or otherwise)
Hi, Can anyone please help me with how I could add labels with the value for each bar in a barchart? (similar to how data labels can be added in Excel) I have done a lot of searching but havent been lucky. Thanks, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Data-Labels-in-a-barchart-Lattice-or-otherwise-tp2278027p2278027.html Sent from the R help mailing list archive at Nabble.com. __ 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] Data Labels in a barchart (Lattice or otherwise)
Thank You David. Yes, I am using the lattice barchart and have managed to add data labels, however, they tend to be on the tip of each bar and are difficult to read as they are partially on the bar. Any help would be greatly appreciated. This is the code I am using: levels(PR_SUMMARY$Bucket)=c(0-3 months,3-9 months,9-15 months,15-18 months) barchart(PrimaryReason ~ cInteractions| Bucket + Type, data = PR_SUMMARY, layout = c(4, 2),col=lightgreen,main=COMPARISON - PRIMARY REASON, sub=L R,xlab=Number of Customers,ylab=Primary Reasons, auto.key = list(title = COMPARISON - PRIMARY REASON,columns=2,points = FALSE, rectangles = TRUE,space= right ),scales = list(x = list(abbreviate=TRUE,minlength=5,rot=45)), panel = function(x,y,subscripts,groups,...){ panel.barchart(x,y,...) ltext(x,y,label=round(PR_SUMMARY$cInteractions,1), cex=.99,rot=45) border=transparent}) I dont really understand the ltext part and found it with some other code, but it works. Thanks again, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Data-Labels-in-a-barchart-Lattice-or-otherwise-tp2278027p2278646.html Sent from the R help mailing list archive at Nabble.com. __ 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] Calculating Summaries for each level of a Categorical variable
Hi Corey, Thanks so much for this. However, I get this error for tapply - Error in tapply(RT, RT$R, fun=WA): arguments must have same length. Any idea how to get around this? Thanks again, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Calculating-Summaries-for-each-level-of-a-Categorical-variable-tp2269349p2269815.html Sent from the R help mailing list archive at Nabble.com. __ 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] Calculating Summaries for each level of a Categorical variable
Hi Christos, Thanks for this. I had a look at Summary.Forumla in the Hmisc package and it is extremely complicated for me. Still trying to decipher how I could use it. Regards, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Calculating-Summaries-for-each-level-of-a-Categorical-variable-tp2269349p2269816.html Sent from the R help mailing list archive at Nabble.com. __ 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] Calculating Summaries for each level of a Categorical variable
Hi, I have a dataset which has a categorical variable R,a count variable C (integer) and 4 or more numeric variables (A,T,W,H - integers) containing measures for R. I would like to summarize each level of the variable R by the average for A,T,W and H. I have written a function to calculate weighted averages using C as the weight and this is given below. The function works perfectly but how do I add the additional dimension I require to this function? Dataset: RT= R A T W H R1 10 20 20 10 R2 60 20 50 10 R3 45 10 20 50 R4 68 50 20 10 R1 73 20 40 46 R3 25 30 10 54 R3 36 90 20 10 R2 29 10 30 30 # FUNCTION TO CALCULATE THE WEIGHTED AVERAGE FOR A WEIGHTED BY C WA-function(A,C) { sp_A-c(A %*% C) sum_C-sum(C) WA-sp_A/sum_C return(WA) } I am trying to incorporate the additional step of calculating the weighted average of A,T,W and H for each level of R. Need help with this. Thanks in advance! Raoul -- View this message in context: http://r.789695.n4.nabble.com/Calculating-Summaries-for-each-level-of-a-Categorical-variable-tp2269349p2269349.html Sent from the R help mailing list archive at Nabble.com. __ 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] Weighted Average application on Summary Dataset
Hi, I have 2 huge datasets - May and Jun - a miniscule sample of one is given below. I am trying to do 2 things with these datasets. I need to verify if the weighted average of variable A for a Reason in Jun is same/different from the same for May. To do this I am first computing the weighted average for each SubReason using a function I wrote. Where I need help is applying the function on both the datasets to arrive at weighted averages for each SubReason. Then, I would like to know what the best way would be, to compare the weighted average for a sub reason across 2 datasets to be able to state that there is a difference - t-test,ANOVA? Would greatly appreciate any help!! The function I wrote for weighted average computation is given below the dataset. One of the datasets: Reason SubReasonA N A SR11115 29 B SR2734 24 B SR21054 31 A Sr1600 43 A SR31033 60 A Sr11163 30 B SR4732 43 B SR4988 70 A SR3569 25 B SR41073 65 Output I require: R SR WA_A N (Sum of N) A SR1912.0098 102 SR3896.5294118 85 B SR2914.3636364 55 SR4957.1966292 178 (Weighted Average of A for N weights) # FUNCTION TO CALCULATE THE WEIGHTED AVERAGE FOR A WEIGHTED BY N WA-function(A,N) { sp_A-c(A %*% N) sum_N-sum(N) WA-sp_A/sum_N return(WA) } Thanks in advance! Raoul -- View this message in context: http://r.789695.n4.nabble.com/Weighted-Average-application-on-Summary-Dataset-tp2253239p2253239.html Sent from the R help mailing list archive at Nabble.com. __ 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.