[R] Help with CrossTable
Hi, I am using the CrossTable function from library(gmodels). x=unique(data[,c("L1","L1_Revenues","RIC")]) L1_Classification=CrossTable(x$L1,x$L1_Revenues,exclude = c("NA","","0%","0"),prop.r=FALSE,prop.c=FALSE,prop.t=FALSE,prop.chisq=FA LSE,dnn=c("L1_Classification","Revenue")) What I would like to do is to get the out put in excel. Also I do not want the output displayed on the console. Is there some way of doing this? sessionInfo() R version 2.7.1 (2008-06-23) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] gmodels_2.14.1 xlsReadWrite_1.3.2 loaded via a namespace (and not attached): [1] gdata_2.4.2 gtools_2.5.0 MASS_7.2-42 Thank you, Ravi This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ 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] Break up a data frame
Hi R users, I have a dataframe in the below format xyz 01/03/200715.25USD xyz 01/04/200715.32USD xyz 01/02/200823.22USD abc 01/03/200745.2 EUR abc 01/04/200745.00EUR abc 01/02/200868.33EUR I want to change the above data into the below format xyz 01/03/200715.25USD abc 01/03/200745.2 EUR xyz 01/04/200715.32USD abc 01/04/200745.00EUR xyz 01/02/200823.22USD abc 01/02/200868.33EUR Any help would be welcome Thank you Ravi This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ 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] Replace with previous value
Hi R, I have a dataframe with dim(test) [1] 435150 4 class(test) [1] "data.frame" In the third column every time a number with "-" appears I need to replace with previous value I am using the following code s=which(substr(as.character(test[,3]),1,1)=="-") for(i in 1:length(s)) test[s[i],3] = test[s[i]-1,3] Is there a faster way of doing this? sessionInfo() R version 2.5.1 (2007-06-27) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" [7] "base" Thanks in advance Ravi Shankar S This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ 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] Range across a List
Hi R, I have a list > class(pp2) [1] "list" > length(pp2) [1] 1244 It is in the below format RIC Trade.Date Close.Price Currency.Code Convertion.Rate New.Price ABCD.SZ 2008/02/29 15.30CNY 0.1408 2.154240 ABCD.SZ 2008/01/31 15.27CNY 0.1392 2.125584 ABCD.SZ 2007/12/31 14.88CNY 0.1371 2.040048 ABCD.SZ 2007/11/30 11.07CNY 0.1357 1.502199 ABCD.SZ 2007/10/31 10.89CNY 0.1340 1.459260 ABCD.SZ 2007/09/28 12.77CNY 0.1334 1.703518 I want to find the range of pp2$New.Price for length(pp2) for each date Any help would be appreciated Thanks in advance Ravi This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ 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] Range across a List
To add more clarity to my question My data pp2 is a list (pp2[[1]]) RIC Trade.Date Close.Price Currency.Code Convertion.Rate New.Price ABCD.SZ 2008/02/29 15.30 CNY 0.1408 2.154240 ABCD.SZ 2008/01/31 15.27 CNY 0.1392 2.040048 ABCD.SZ 2007/11/30 11.07 CNY 0.1357 1.502199 ABCD.SZ 2007/10/31 10.89 CNY 0.1340 1.459260 ABCD.SZ 2007/09/28 12.77 CNY 0.1334 1.703518 (pp2[[2]]) RIC Trade.Date Close.Price Currency.Code Convertion.Rate New.Price PQRS.SZ 2008/02/299.27 CNY 0.1408 1.305216 PQRS.SZ 2008/01/318.07 CNY 0.1392 1.123344 PQRS.SZ 2007/12/318.76 CNY 0.1371 1.200996 PQRS.SZ 2007/11/306.43 CNY 0.1357 0.872551 PQRS.SZ 2007/10/316.80 CNY 0.1340 0.911200 PQRS.SZ 2007/09/287.94 CNY 0.1334 1.059196 And so on till (pp2[[1244]]) Each of pp2[[i]] is a data frame. For each date I need to find the range of New.Price across the list i.e.for 2008/02/29 it would be max(pp2[[i]]$New.Price[1])-min(pp2[[i]]$New.Price[1]) where i ranges from 1 to 1244 Thank you, Ravi -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Thursday, March 27, 2008 2:12 AM To: Ravi S. Shankar Subject: Re: [R] Range across a List >From: "Ravi S. Shankar" <[EMAIL PROTECTED]> >Date: 2008/03/26 Wed PM 03:28:52 CDT >To: [EMAIL PROTECTED] >Subject: [R] Range across a List i think it's a dataframe ( it looks like one ) or convert it to one if it's not and then I think below should work. temp<-lapply(split(pp2,pp2$Trade.Date), function(.df) { data.frame(.df$Trade.Date[1],range(.df$New.Price)) }) result<-do.call(rbind,temp) test it though because i didn't. >Hi R, >I have a list >> class(pp2) >[1] "list" > >> length(pp2) >[1] 1244 > >It is in the below format > RIC Trade.Date Close.Price Currency.Code Convertion.Rate New.Price >ABCD.SZ 2008/02/29 15.30CNY 0.1408 2.154240 >ABCD.SZ 2008/01/31 15.27CNY 0.1392 2.125584 >ABCD.SZ 2007/12/31 14.88CNY 0.1371 2.040048 >ABCD.SZ 2007/11/30 11.07CNY 0.1357 1.502199 >ABCD.SZ 2007/10/31 10.89CNY 0.1340 1.459260 >ABCD.SZ 2007/09/28 12.77CNY 0.1334 1.703518 > >I want to find the range of pp2$New.Price for length(pp2) for each date >Any help would be appreciated > >Thanks in advance >Ravi > > >This e-mail may contain confidential and/or privileged i...{{dropped:10}} > >__ >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. This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ 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] Range across a List
I did the following DF<-do.call(rbind, pp2) DF1=na.omit(DF) DF1[,2]=as.Date(DF1[,2]) str(DF) 'data.frame': 18660 obs. of 6 variables: I tried the following code temp<-lapply(split(DF1,DF1$Trade.Date), function(.df) { + data.frame(DATE=.df$Trade.Date,RANGE=max(.df$New.Price)-min(.df$New.Pric e)) + }) temp[[1]][1:5,] DATERANGE 1 2006-12-29 1276.670 2 2006-12-29 1276.670 3 2006-12-29 1276.670 4 2006-12-29 1276.670 5 2006-12-29 1276.670 What am I doing wrong? I also tried the below and that seemed to give me the range nn=tapply(DF1$New.Price, DF1$Trade.Date, range) > head(nn) $`2006-12-29` [1]0.0074638 1276.6772880 $`2006-12-31` [1] 4.673445 227.60 $`2007-01-31` [1]0.0030772 1255.2080450 $`2007-02-28` [1]0.003978 1316.638200 $`2007-03-29` [1] 5.25585 216.2 $`2007-03-30` [1]0.0047214 1266.825 Thanks Ravi Shankar S -Original Message- From: jim holtman [mailto:[EMAIL PROTECTED] Sent: Thursday, March 27, 2008 2:49 AM To: Ravi S. Shankar Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: [R] Range across a List I think something like this should work. I will give you the range for each date across all the data: x <- do.call(rbind, pp2) tapply(x$New.Price, x$Trade.Date, range) On 3/26/08, Ravi S. Shankar <[EMAIL PROTECTED]> wrote: > To add more clarity to my question > > My data pp2 is a list > > (pp2[[1]]) > RIC Trade.Date Close.Price Currency.Code Convertion.Rate New.Price > ABCD.SZ 2008/02/29 15.30 CNY 0.1408 > 2.154240 > ABCD.SZ 2008/01/31 15.27 CNY 0.1392 > 2.040048 > ABCD.SZ 2007/11/30 11.07 CNY 0.1357 > 1.502199 > ABCD.SZ 2007/10/31 10.89 CNY 0.1340 > 1.459260 > ABCD.SZ 2007/09/28 12.77 CNY 0.1334 > 1.703518 > > (pp2[[2]]) > RIC Trade.Date Close.Price Currency.Code Convertion.Rate > New.Price > PQRS.SZ 2008/02/299.27 CNY 0.1408 1.305216 > PQRS.SZ 2008/01/318.07 CNY 0.1392 1.123344 > PQRS.SZ 2007/12/318.76 CNY 0.1371 1.200996 > PQRS.SZ 2007/11/306.43 CNY 0.1357 0.872551 > PQRS.SZ 2007/10/316.80 CNY 0.1340 0.911200 > PQRS.SZ 2007/09/287.94 CNY 0.1334 1.059196 > > And so on till (pp2[[1244]]) > > Each of pp2[[i]] is a data frame. For each date I need to find the range > of New.Price across the list > i.e.for 2008/02/29 it would be > max(pp2[[i]]$New.Price[1])-min(pp2[[i]]$New.Price[1]) where i ranges > from 1 to 1244 > > Thank you, > Ravi > > > > > > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: Thursday, March 27, 2008 2:12 AM > To: Ravi S. Shankar > Subject: Re: [R] Range across a List > > >From: "Ravi S. Shankar" <[EMAIL PROTECTED]> > >Date: 2008/03/26 Wed PM 03:28:52 CDT > >To: [EMAIL PROTECTED] > >Subject: [R] Range across a List > > i think it's a dataframe ( it looks > like one ) or convert it to > one if it's not and then I think below should > work. > > temp<-lapply(split(pp2,pp2$Trade.Date), function(.df) > { > data.frame(.df$Trade.Date[1],range(.df$New.Price)) > }) > > result<-do.call(rbind,temp) > > test it though because i didn't. > > > >Hi R, > >I have a list > >> class(pp2) > >[1] "list" > > > >> length(pp2) > >[1] 1244 > > > >It is in the below format > > RIC Trade.Date Close.Price Currency.Code Convertion.Rate > New.Price > >ABCD.SZ 2008/02/29 15.30CNY 0.1408 2.154240 > >ABCD.SZ 2008/01/31 15.27CNY 0.1392 2.125584 > >ABCD.SZ 2007/12/31 14.88CNY 0.1371 2.040048 > >ABCD.SZ 2007/11/30 11.07CNY 0.1357 1.502199 > >ABCD.SZ 2007/10/31 10.89CNY 0.1340 1.459260 > >ABCD.SZ 2007/09/28 12.77CNY 0.1334 1.703518 > > > >I want to find the range of pp2$New.Price for length(pp2) for each date > >Any help would be appreciated > > > >Thanks in advance > >Ravi > > > > > >This e-mail may contain confidential and/or privileged > i...{{dropped:10}} > > > >__ > >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. > > This e-mail may contain confidential and/or privileged...{{dropped:30}} __ 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] convert matrix to dataframe with repeating row names
Hi R, I have a matrix x with repeating row names. > dim(x) [1] 862 19 zz<-matrix(0,4,4) rownames(zz)=c("a","a","b","b") data.frame(zz) (?) I need to use x in a linear regression lm(as.formula(paste("final_dat[,5]~",paste(colnames(x),collapse="+"))),x ) this gives me a error Error in model.frame.default(formula = as.formula(paste("final_dat[,5]~", : 'data' must be a data.frame, not a matrix or an array > sessionInfo() R version 2.7.1 (2008-06-23) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] xlsReadWrite_1.3.2 Thanks in advance Ravi This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ 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] Read in date fomat while colClasses="character"
Hi R users, I am using xlsReadWrite to read a particular excel file. In one of the columns I have dates ( say col=5). Now date column is read by default as numeric. So I used dateTimeAs= "isodatetime". This enables reading in the date format. However in the earlier column (say col=1) I have data which however starts from row 10. So to read data from column one I use colClasses="character". But this overrules the isodatetime. Now is there someway I can read in date format while keeping colClasses="character" Thanks for your help Ravi [[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.
[R] write text file as output without quotes
Hi R, When I use the below to write the text file try=data.frame(rep("a",5), rep("b",5)) write.table(try,"z:\\try.txt",row.names=F,col.names=F,sep="\t") the output contains two columns with quotes! Is there a way to write without quotes? I tried try[,1]=noquote(try[,1]) try[,2]=noquote(try[,2]) Thank you, Regards, Ravi Shankar This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ 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] combine words and dataframe
Hi R, I am trying to get an output like this Hi Hello 1 a b 2 a b 3 a b 4 a b 5 a b And write it as a text file cat(paste("Hi",sep='\n',"Hello")) gives me Hi Hello And when I try cat(paste("Hi",sep='\n',"Hello"),sep='\n',data.frame(rep("a",3),rep("b", 3))) I get an error! Thanks in advance for your help! Regards Ravi This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ 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 the trading days
Hi R, I have two columns of date in a CSV file in the below format 29-Dec-06 25-Jan-07 29-Dec-06 25-Jan-07 29-Dec-06 25-Jan-07 2-Jan-0725-Jan-07 2-Jan-0725-Jan-07 2-Jan-0725-Jan-07 I read in R using dat<-read.csv("Z:\\data.csv"). > class(dat[,1]) [1] "factor" So I use dat[,1]=as.Date(as.character(dat[,1]),"%d-%b-%YY") > class(dat[,1]) [1] "Date" But when I do > fix(dat) Warning: class discarded from column 'Date' Also > dat[1,1] [1] NA My task is to compute the number of trading days between the two dates in col A and Col B. Any help would be appreciated! Thank you, Ravi Shankar S This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ 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.