Re: [R] how to combine unequal rows and columns in R

2013-01-24 Thread arun
Hi, Another way might be: df1 <- merge(crosspries[[1]], crosspries[[2]], all=TRUE) template1<-read.table(text=outer(unique(df1[,1]),unique(df1[,2]),FUN=paste),sep="",stringsAsFactors=F)   res<-merge(df1,template1,by.x=c("Product","Year_Month"),by.y=c("V1","V2"),all=TRUE) res1<-do.call(rbind,split(

Re: [R] how to combine unequal rows and columns in R

2013-01-24 Thread arun
HI, If you use ?join(), it will preserve the order of the first dataframe. library(plyr) df3<-rbind.fill(crosspries[[1]],crosspries[[2]]) template2<-read.table(text=outer(unique(df3[,1]),unique(df3[,2]),FUN=paste),sep="",stringsAsFactors=F) names(template2)<-names(df3[1:2]) res1<-join(template2,df

Re: [R] From table to data.frame

2013-01-24 Thread arun
Hi, set.seed(25)  Z<-rnorm(50,0.5,1) res<- as.data.frame(table(cut(Z,seq(0,1,by=0.05),labels=seq(0.05,1,by=0.05))) )  head(res)  # Var1 Freq #1 0.05    1 #2  0.1    1 #3 0.15    0 #4  0.2    0 #5 0.25    0 #6  0.3    1 A.K. - Original Message - From: Wim Kreinen To: r-help@r-project.

Re: [R] From table to data.frame

2013-01-24 Thread arun
Hi, In the ?cut(), you can specify `labels`. #or, you can try: test.df<-read.table(text="     Var1 Freq 1    (0,0.05]  68 2  (0.05,0.1]  87 3  (0.1,0.15]  85 4  (0.15,0.2]  72 5  (0.2,0.25]  65 6  (0.25,0.3]  73 7  (0.3,0.35]  74 ",sep="",header=T,stringsAsFactors=F) test.df[,1]<-as.numeric(gsub("

Re: [R] Question on matrix calculation

2013-01-24 Thread arun
Hi, You could also use: mat <- matrix(1:15, 5) set.seed(5) match_df <- data.frame(Seq = 1:5, criteria = sample(letters[1:5], 5, replace = T),stringsAsFactors=F) library(plyr) res<-daply(as.data.frame(mat),.(match_df$criteria),colSums) res #match_df$criteria V1 V2 V3   #   

Re: [R] sorting/grouping/classification problem?

2013-01-24 Thread arun
HI, If I understand your question, "dat3" is not you wanted. Is  it something like this you wanted? library(reshape2)  dcast(dat,rrt~Mnd,value.var="Result") #   rrt   0   3    6    9 #1 0.35  NA  NA 0.05   NA #2 0.36  NA  NA   NA 0.06 #3 0.44  NA  NA 0.40   NA #4 0.45 0.1 0.2   NA 0.60 #5 0.46  N

Re: [R] How to drop unused factors in faceted R ggplot boxplot?

2013-01-24 Thread arun
HI, You can get the plot in the same window, but the width seems to be a problem. stest<- read.table(text="     site year conc     south 2001 5.3     south 2001 4.67     south 2001 4.98     south 2002 5.76     south 2002 5.93     north 2001 4.64     north 2001 6.32     north 2003 11.5     north 200

Re: [R] ifelse to speed up loop?

2013-01-24 Thread arun
Hi, You could do this: test<-read.table(text="   V1 1  1 2  1 3  1 4  2 5  2 6  2 7  1 8  1 9  1 10  2 11  2 12  2 ",sep="",header=T)  test$Group<-cumsum(abs(c(0,diff(test[,1]+1 test #   V1 Group #1   1 1 #2   1 1 #3   1 1 #4   2 2 #5   2 2 #6   2 2 #7   1 3 #   1  

Re: [R] ifelse to speed up loop?

2013-01-24 Thread arun
HI, This might be better. test$group<-cumsum(abs(c(1,diff(test[,1] A.K. - Original Message - From: Jeffrey Fuerte To: r-help@r-project.org Cc: Sent: Thursday, January 24, 2013 4:20 PM Subject: [R] ifelse to speed up loop? Hello, I'm not sure how to explain what I'm looking for

Re: [R] ifelse to speed up loop?

2013-01-24 Thread arun
Hi, I guess you meant: c(TRUE, cumsum( v[-length(v)] != v[-1] ))  [1] 1 0 0 1 1 1 2 2 2 3 3 3 this:  cumsum(c(TRUE, v[-length(v)] != v[-1] )) # [1] 1 1 1 2 2 2 3 3 3 4 4 4 A.K. - Original Message - From: William Dunlap To: arun ; Jeffrey Fuerte Cc: R help Sent: Thursday, January 24

Re: [R] How to extract elements from vector in reverse order?

2013-01-24 Thread arun
Hi,  x<-1:80 y<- x[-(1:77)] y #[1] 78 79 80 #or ?tail() #already suggested If you want only the last element,, library(pastecs) last(x) #[1] 80 A.K. - Original Message - From: hp wan To: r-help@r-project.org Cc: Sent: Thursday, January 24, 2013 7:23 PM Subject: [R] How to extrac

Re: [R] ifelse to speed up loop?

2013-01-24 Thread arun
1$group2<-cumsum(c(TRUE, v[-length(v)] != v[-1] ))# works  test1 #  V1 group group2 #1   a 1  1 #2   a 1  1 #3   a 1  1 #4   b 2  2 #5   b 2  2 #6   b 2  2 #7   a 3  3 #8   a 3  3 #9   a 3  3 #10  d 5  4 #11  d

Re: [R] how to delete the null elements in list

2013-01-25 Thread arun
Or, suu[unlist(lapply(suu,length)!=0)] #[[1]] #[1] 1 2 #[[2]]  #    [,1] [,2] #[1,]    1    3 #[2,]    2    4 A.K. - Original Message - From: Rui Barradas To: Tammy Ma Cc: "r-help@r-project.org" Sent: Friday, January 25, 2013 8:15 AM Subject: Re: [R] how to delete the null elements

Re: [R] Help with adding 'dates' string as rownames to matrix

2013-01-25 Thread arun
Hi, Try this: Dates1<-c("2005-04-01 BST","2005-04-04 BST","2005-04-05 BST") Dates2<-as.Date(gsub("\\s+\\w+$","",Dates1))  mat1<-matrix(1:9,nrow=3) library(zoo)  z1<-zoo(mat1,Dates2)  z1 #    #2005-04-01 1 4 7 #2005-04-04 2 5 8 #2005-04-05 3 6 9 #if you use:   mat2<-matrix(1:9,nrow=3)

Re: [R] resizing data

2013-01-25 Thread arun
which(x==1,arr.ind=TRUE)    row col #[1,] 1   7  y<-t(matrix(x,nrow=546))  which(y==1,arr.ind=TRUE) # row col #[1,] 341 328 A.K. - Original Message - From: emorway To: r-help@r-project.org Cc: Sent: Friday, January 25, 2013 5:29 PM Subject: Re: [R] resizing data I played a

Re: [R] sorting/grouping/classification problem?

2013-01-25 Thread arun
Hi, Your question is bit confusing to me.   When you say that "which rrts are the same, and which are the  new ones", to me it looks like "0.35, 0.36" are new addition to Mnd at time points 6 and 9. Extending Dennis' solution: Just for understanding the problem:  vec1<-c(0.45,0.48,1.24,1.22,0.44,

Re: [R] resizing data

2013-01-25 Thread arun
HI, It's not clear why you wanted to take the transpose and resize it afterwards. It could be done in one step as David suggested. Suppose, you wanted to get the result after you transposed the matrix: x<-matrix(1:64,8) x1<-t(x) matrix(unlist(split(x1,row(x1))),ncol=4,byrow=T) #  [,1] [,2] [

Re: [R] resizing data

2013-01-25 Thread arun
Hi, Inline: - Original Message - From: emorway To: r-help@r-project.org Cc: Sent: Friday, January 25, 2013 5:29 PM Subject: Re: [R] resizing data I played around with your example on the smaller dataset, and it seemed like it was doing what I wanted.  However, applying it to the larg

Re: [R] Removal of columns from matrix where all values of the column are identical.

2013-01-25 Thread arun
Hi, May be this helps: Matrix[,colSums(diff(Matrix))!=0] # [,1] [,2] [,3] #[1,]    1    5    5 #[2,]    2    4    1 #[3,]    3    3    4 #[4,]    4    2    3 #[5,]    5    1    2 A.K. - Original Message - From: Benjamin Ward (ENV) To: "r-help@r-project.org" Cc: Sent: Friday, Jan

Re: [R] Pass vector as multiple parameters (as in python f(*x))

2013-01-25 Thread arun
HI, You could use ?Reduce() also in the second case: lapply(vs,function(v){Reduce(f,as.list(v))}) #[[1]] #[1] 10 #[[2]] #[1] 6 #[[3]] #[1] 1 A.K. - Original Message - From: Carlos Pita To: r-help@r-project.org Cc: Sent: Friday, January 25, 2013 7:46 PM Subject: Re: [R] Pass vector

Re: [R] Removal of columns from matrix where all values of the column are identical.

2013-01-25 Thread arun
Hi, I guess this should also work:  Matrix[,apply(Matrix,2,function(x) all(c(TRUE,x[-length(x)]!=x[-1])))] # [,1] [,2] [,3] #[1,]    1    5    5 #[2,]    2    4    1 #[3,]    3    3    4 #[4,]    4    2    3 #[5,]    5    1    2 A.K. - Original Message - From: Benjamin Ward (ENV)

Re: [R] Removal of columns from matrix where all values of the column are identical.

2013-01-26 Thread arun
,] "d"  "e"  "a"  "e" #[2,] "a"  "e"  "d"  "c" #[3,] "e"  "b"  "c"  "e" #[4,] "d"  "d"  "d"  "a" #[5,] "b"  "e"  &

Re: [R] Loading data into R

2013-01-26 Thread arun
Hi, myData[]<-lapply(myData,as.character)  as.matrix(myData) # V1  V2   V3  V4  V5 #[1,] "a" "b"  "c" "d" "f" #[2,] "2" "4"  ""  ""  "7" #[3,] ""  ""   ""  ""  "" #[4,] ""  "34" ""  "4" "" #[5,] "2" ""   "4" ""  "4" str(as.matrix(myData)) # chr [1:5, 1:5] "a" "2" "" "" "2" "b" "4" "" "34"

Re: [R] a function more appropriate than 'sapply'?

2013-01-26 Thread arun
Hi, May be this helps:  wells<-read.table("wells.txt",header=FALSE,stringsAsFactors=F)  wells2<-wells[-grep(".*\\_.*\\_",wells[,1]),]   head(wells2)   #   V1 V2 #1  w7_1  0 #2 w11_1  0 #3 w12_1  0 #4 w13_1  0 #5 w14_1  0 #6 w15_1  0 wellsNew<-wells[grep(".*\\_.*\\_",wells[,1]),]  head(wellsNew

Re: [R] a function more appropriate than 'sapply'?

2013-01-26 Thread arun
HI, ?grep() found to be a bit faster. system.time({ indx<-grep(".*\\_.*\\_",wells[,1]) wells2<-wells[-indx,] wellsNew<-wells[indx,]}) # user  system elapsed  # 0.024   0.000   0.023  system.time({  w.sub <- gsub("[^_]+","",wells[,1])   u.2 <- which(w.sub=="__")   u.1 <- which(w.sub=="_")  w.u

Re: [R] Converting column of strings to boolean

2013-01-27 Thread arun
Hi, Anther possibility may be to use: library(MatrixModels)   model.Matrix(~d-1,sparse=FALSE) #7 x 4 Matrix of class "ddenseModelMatrix"  # dblue dgreen dred dyellow #1     0  0    1   0 #2 0  1    0   0 #3 0  0    1   0 #4 1  0    0   0 #5 0 

Re: [R] Loops

2013-01-27 Thread arun
Hi, You could use library(plyr) as well library(plyr) pnew<-colSums(aaply(laply(split(as.data.frame(p),((1:nrow(as.data.frame(p))-1)%/% 25)+1),as.matrix),c(2,3),function(x) x)) res<-rbind(t(pnew),colSums(p)) row.names(res)<-1:nrow(res) res<- 100-100*abs(res/rowSums(res)-(1/3)) A.K. - Orig

Re: [R] Removing values containing a specific character

2013-01-27 Thread arun
5.5 million rows, and grep doesn't seem to work with over a million rows. Any idea on a workaround? On Sat, Jan 26, 2013 at 9:37 PM, Yasha Podeswa wrote: > Awesome, thanks Arun, that's exactly what I was looking for! > > > On Sat, Jan 26, 2013 at 9:21 PM, arun kirshn

Re: [R] Removing values containing a specific character

2013-01-27 Thread arun
elapsed #  1.732   0.108   1.844  system.time(dfNew1<-df[grep("\\w+",df$names),]) #   user  system elapsed #  0.896   0.024   0.923  system.time(dfNew2<- df[df$names!="",]) #   user  system elapsed  # 0.460   0.028   0.490 A.K. From:

Re: [R] Thank you your help.

2013-01-28 Thread arun
228 32.4000 #7 HM001  1229  NA #8 HM001  1230 27.4000 #9 HM001  1231 22.4236 #Based on the variance, You could set up some limit, for example 50 and use: tempnew$WEIGHT<- ifelse(tempnew$WEIGHT>50,NA,tempnew$WEIGHT) A.K. From: 남윤주 To: arun Sent:

Re: [R] Thank you your help and one more question.

2013-01-28 Thread arun
New,imputNew$CTIME),function(x) {x$WEIGHT<-mean(x[,3]);head(x,1)})) row.names(res3)<-1:nrow(res3) identical(res1,res2) #[1] TRUE  identical(res1,res3) #[1] TRUE A.K. From: 남윤주 To: arun Sent: Monday, January 28, 2013 9:47 PM Subject: Re: Thank you your hel

Re: [R] Thank you your help and one more question.

2013-01-28 Thread arun
Names = c("ID", "CTIME", "WEIGHT" ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", &q

Re: [R] Thank you your help and one more question.

2013-01-28 Thread arun
uot;6", "7", "8", "9", "10", "11", "12", "13", "14", "15"))) #It could be combined by: do.call(rbind, imput1_2_3)# But if you do this the total number or rows will be the sum of the number of rows of each

Re: [R] Pivot

2013-01-29 Thread arun
HI, You could try: dat1<-read.table(text=" X Z x1    102 x2    102 x2    102 x2    77 x3    23 ",sep="",header=T,stringsAsFactors=F)  res1<-tapply(dat1[,1],list(dat1[,1],dat1[,2]),length) res1[apply(res1,2,function(x) is.na(x))]<-0  colnames(res1)<-paste("Z.",colnames(r

Re: [R] I succeed to get result dataset.

2013-01-29 Thread arun
]<-NA Hope it helps. A.K. From: 남윤주 To: arun Sent: Tuesday, January 29, 2013 3:36 AM Subject: Re: I succeed to get result dataset. Arun ~ I have a dfficuliting in using R again. A Dataset 'temp' contatins NA and strange value(like 8 row

Re: [R] NA and Character(0) in List Element

2013-01-29 Thread arun
Hi, May be this helps: x<- list(1:5,NA,20:25,5) names(x)<-1:4  fun1<-function(lst){  lst[lapply(lapply(lst,Filter,f=Negate(is.na)),length)!=0]} fun1(x) #$`1` #[1] 1 2 3 4 5 # #$`3` #[1] 20 21 22 23 24 25 # #$`4` #[1] 5 #or x[lapply(lapply(x,na.omit),length)!=0] A.K. - Original Message --

Re: [R] I think you misunderstood my explantation.

2013-01-29 Thread arun
$REACTIVE_KWH)])< 0)]<-NA A.K. ____ From: 남윤주 To: arun Sent: Tuesday, January 29, 2013 7:42 PM Subject: I think you misunderstood my explantation. Hi, Assume that first CTIME value is '20120101'. It means ACTIVE_KWH measured from  '201201

Re: [R] recoding variables again :(

2013-01-30 Thread arun
Hi, set.seed(125) dat1<-data.frame(BIRTHPLACE=sample(c("AG","AI","AR","BE","ZH","USA","GER","ESP"),20,replace=TRUE),RES_STA=sample(LETTERS[c(1:3,24:25)],20,replace=TRUE)) dat1$VARNEW<-ifelse(dat1$RES_STA=="X" & dat1$BIRTHPLACE%in%c("AG","AI","AR","BE","ZH"),"swiss","unknown") A.K. - Origi

Re: [R] I think you misunderstood my explantation.

2013-01-30 Thread arun
#changed to NA #Similarly for REACTIVE_KWH Hope this helps. A.K. From: 남윤주 To: arun Sent: Wednesday, January 30, 2013 12:51 AM Subject: Re: I think you misunderstood my explantation. Oh, I forgot to ask about those code. Can u expain what dose that mean?

Re: [R] Fastest way to compare a single value with all values in one column of a data frame

2013-01-30 Thread arun
Hi, I guess you could also use:  x[match(min(x$a),x$a[x$a To: r-help Cc: Sent: Tuesday, January 29, 2013 4:11 PM Subject: [R] Fastest way to compare a single value with all values in one column of a data frame Hello! I have a large data frame x: x<-data.frame(item=letters[1:5],a=1:5,b=11:15)

Re: [R] substring from behind

2013-01-30 Thread arun
Hi, You could use: dat1<-data.frame(col1=c("Mercedes_02352","Audi_03555")) dat1[,1]<-as.numeric(gsub(".*_\\d{1}(.*)\\d{1}$","\\1",dat1[,1])) #if the number of digits are the same  dat1 #  col1 #1  235 #2  355 A.K. - Original Message - From: Mat To: r-help@r-project.org Cc: Sent: Wedne

Re: [R] Fastest way to compare a single value with all values in one column of a data frame

2013-01-30 Thread arun
HI, Sorry, my previous solution doesn't work. This should work for your dataset: set.seed(1851) x<- data.frame(item=sample(letters[1:5],20,replace=TRUE),a=sample(1:15,20,replace=TRUE),b=sample(20:30,20,replace=TRUE),stringsAsFactors=F) y<- data.frame(item="f",a=3,b=10,stringsAsFactors=F)  x[x$a%i

Re: [R] Fastest way to compare a single value with all values in one column of a data frame

2013-01-30 Thread arun
tem="z",a=3,b=10,stringsAsFactors=F) x[intersect(which(x$a < y$a),which.min(x$a)),]  #  item a  b #17    c 1 48  x[x$a==which.min(x$a[x$a To: arun Cc: R help ; Jessica Streicher Sent: Wednesday, January 30, 2013 1:49 PM Subject: Re: [R] Fastest way to compare a single value wit

Re: [R] Locate Patients who have multiple high blood pressure readings

2013-01-31 Thread arun
Hi, May be this helps: #dd res<-data.frame(Include=with(subset(dd,OBS_TYPE == "SBP" & Blood_Pressure >= 140|OBS_TYPE=="DBP" & Blood_Pressure>=90),apply(tapply(Blood_Pressure,list(PT_ID,OBS_TYPE),length)>=2,1,any,na.rm=T))) res  #    Include #1900    TRUE #2900   FALSE #3900   FALSE A.K. ---

Re: [R] expand.grid on contents of a list

2013-02-01 Thread arun
Hi, expand.grid(X) # Var1 Var2 Var3 #1111 #2211 #3121 #4221 #5112 #6212 #7122 #8222 expand.grid(1:2,1:2,1:2) # Var1 Var2 Var3 #1111 #2211 #3121 #4

Re: [R] Filter according to the latest data

2013-02-01 Thread arun
Hi, Perhaps, (#Untested) do.call(rbind,lapply(split(dat1,dat1$No),function(x) tail(x,1))) #or library(plyr) ddply(dat1,.(No), function(x) x[nrow(x),]) A.K. - Original Message - From: Mat To: r-help@r-project.org Cc: Sent: Friday, February 1, 2013 3:04 AM Subject: [R] Filter according

Re: [R] Transforming 4x3 data frame into 2 column df in R

2013-02-01 Thread arun
Hi, library(reshape) foo$id<-row.names(foo)  melt(foo,id.var="id")    id variable  value #1   n    w 1.51550092 #2   q    w 0.09977303 #3   r    w 1.17083866 #4   n    x 1.43375720 #5   q    x 0.81737610 #6   r    x 1.24693470 #7   n    y 1.27916240 #8   q    y 1

Re: [R] Summary of data for each year

2013-02-01 Thread arun
Hi, You could use: creek <- read.csv("creek.csv",sep="\t")  colnames(creek) <- c("date","flow") creek$date <- as.Date(creek$date, "%m/%d/%Y") creek1 <- within(creek, year <- format(date, '%Y')) library(data.table)  creek2<- data.table(creek1)   creek2[,list(MEAN=.Internal(mean(flow)),MEDIAN=medi

Re: [R] cumulative sum by group and under some criteria

2013-02-01 Thread arun
To: r-help@r-project.org Cc: Sent: Friday, February 1, 2013 12:19 PM Subject: Re: [R] cumulative sum by group and under some criteria Thank you very much for your reply. Your code work well with this example. I modified a little to fit my real data, I got an error massage. Error in split.def

Re: [R] cumulative sum by group and under some criteria

2013-02-01 Thread arun
k right. I didn't  A.K. - Original Message - From: Zjoanna To: r-help@r-project.org Cc: Sent: Friday, February 1, 2013 12:19 PM Subject: Re: [R] cumulative sum by group and under some criteria Thank you very much for your reply. Your code work well with this example. I modified

Re: [R] vectorisation

2013-02-02 Thread arun
Hi, Not sure this helps: ml <- data.frame(matrix(sample(1:50,80, replace=TRUE),20,4)) mm <- apply(ml, 2, cumsum) starts<- data.frame(matrix(0,600,4)) starts1<- data.frame(matrix(0,600,4)) for (i in 1:4){ starts1[,i][mm[,i]] <-1 } starts2<-as.data.frame(do.call(cbind,lapply(1:4,function(i) {star

Re: [R] cumulative sum by group and under some criteria

2013-02-03 Thread arun
--- Original Message - From: Zjoanna To: r-help@r-project.org Cc: Sent: Friday, February 1, 2013 12:19 PM Subject: Re: [R] cumulative sum by group and under some criteria Thank you very much for your reply. Your code work well with this example. I modified a little to fit my real data, I got a

Re: [R] Adding complex new columns to data frame depending on existing column

2013-02-03 Thread arun
Hi, May be this helps: df1<-read.table(text=" V1    V2    V3        V4        V5 V6 chr1 18884  C        C      2  0 chr1 135419 TATACA  T          2  0 chr1 332045 T      TTG      0  2 chr1 453838 T      TAC      2  0 chr1 567652 T        TG      1  0 chr1 602541 TTTA    T          2  0 ",h

Re: [R] reshape help

2013-02-04 Thread arun
Hi, Try this: library(reshape2) dat1<-read.table(text="ID  Dx A    nausea A    diabetes A    kidney_failure A    heart_attack A    fever B    fever B    pneumonia B    heart_attack B    nausea B    cough C    kidney_failure C    nausea C    foot_pain",header=T,stringsAsFactors=F,sep="")  dcast(dat

Re: [R] Script for conditional sums of vectors

2013-02-04 Thread arun
Hi, library(reshape2) dcast(DF,a~b,value.var="c",sum) #  a   1   2   3   4 #1 1 900 100 500 300 #2 2 300 900 200 100 A.K. - Original Message - From: Benjamin Gillespie To: "r-help@r-project.org" Cc: Sent: Monday, February 4, 2013 4:29 AM Subject: [R] Script for conditional sums of vect

Re: [R] cumulative sum by group and under some criteria

2013-02-05 Thread arun
ssage - From: Zjoanna To: r-help@r-project.org Cc: Sent: Friday, February 1, 2013 12:19 PM Subject: Re: [R] cumulative sum by group and under some criteria Thank you very much for your reply. Your code work well with this example. I modified a little to fit my real data, I got an error massage

Re: [R] If() values in one dataframe then return values from another

2013-02-05 Thread arun
Hi, I am not sure about what your end result should be: >From your code, it looks like you want to replace the rows of dat1 that >contain at least a `3` with the corresponding row of dat2. Or is it only the >cell with number `3` replaced by corresponding row in dat2. dat1<- read.table(text="    

Re: [R] longitudinal study

2013-02-05 Thread arun
 1 #2  1  0  0  0  0 #3  0  0  1  0  1 #4  0  1  0  0  0  identical(dat1,dat2) #[1] TRUE A.K. - Original Message - From: bibek sharma To: arun Cc: Sent: Monday, February 4, 2013 12:54 PM Subject: Re: [R] longitudinal study Dear Arun, I have a data set with dim 1200 by 56 where the var

Re: [R] How to subset a data frame to include only first events

2013-02-05 Thread arun
HI, If the  `Date` column is not ordered: Date1=as.Date(c("01/05/2012","01/07/2012","01/15/2012","01/09/2012","01/14/2012","01/25/2012", "01/08/2012","01/24/2012","01/03/2012"),format="%m/%d/%Y") dat1<-data.frame(ID=rep(1:3,each=3),Date1)  aggregate(Date1~ID,data=dat1,function(x) min(x)) #  ID  

Re: [R] Transform every integer to 1 or 0

2013-02-05 Thread arun
Hi, You could try:  df[df>0]<-1 #or  df[]<-as.integer(df!=0)  df #    V1 V2 V3 #yes  1  0  1 #no   0  1  1 A.K. - Original Message - From: Wim Kreinen To: r-help@r-project.org Cc: Sent: Monday, February 4, 2013 1:33 PM Subject: [R] Transform every integer to 1 or 0 Hello, I have a da

Re: [R] cumulative sum by group and under some criteria

2013-02-05 Thread arun
      2    0.00032 0.0025 #3       2       3    0.01952 0.00048125 res3[res3[,3]<0.01 & res3[,4]<0.01,]  # Group.1 Group.2 cterm1_P1L cterm1_P0H #2       3       2    0.00032     0.0025 Hope it helps. A.K. ____ From: Joanna Zhang To: arun Sent: Tuesday, Febr

Re: [R] adjacency list to non-symmetric matrix

2013-02-05 Thread arun
For converting from first dataset to second, library(reshape2) #dat1 is data dcast(dat1,person~group,value.var="group",length) #  person a b c d #1   Greg 1 0 0 0 #2   Mary 0 1 0 1 #3    Sam 1 1 1 0 #4    Tom 0 1 1 1 A.K. - Original Message - From: Sebastian Haunss To: r-help@r-project.

Re: [R] cumulative sum by group and under some criteria

2013-02-05 Thread arun
m1 n1 cterm1_P1L cterm1_P0H #1 5 4  3  2    0.00032     0.0025 #2 5 5  3  2    0.00032     0.0025 #3 5 6  3  2    0.00032     0.0025 #4 5 7  3  2    0.00032     0.0025 #5 5 8  3  2    0.00032     0.0025 #6 5 9  3  2    0.00032     0.0025 A.K. ____ From: Joanna Zhang To: arun

Re: [R] how to "multiply" list of matrices by list of vectors

2013-02-06 Thread arun
Hi, I got an error message with: vlist <- apply(mm, list) Error in match.fun(FUN) : argument "FUN" is missing, with no default #assuming that vlist <- apply(mm,2,list) mapply("%*%",mlist,vlist[1:2],SIMPLIFY=FALSE) #[[1]]  #    [,1] #[1,]   19 #[2,]   22 #[3,]   25 #[4,]   28 # #[[2]]  #    [,1]

Re: [R] cumulative sum by group and under some criteria

2013-02-06 Thread arun
(n1+2):(maxN-m),0:m1,0:n1, x1:(x1+m-m1),y1:(y1+n-n1)) #  I couldn't find  "m" or "n"          A.K.                           ____ From: Joanna Zhang To: arun Sent: Wednesday, February 6, 2013 12:48 PM Subject: Re: cumulative sum by group a

Re: [R] cumulative sum by group and under some criteria

2013-02-06 Thread arun
3  2    0.00032     0.0025  nrow(resF) #[1] 6300 I am not sure what you want to do with this. A.K. From: Joanna Zhang To: arun Sent: Wednesday, February 6, 2013 10:29 AM Subject: Re: cumulative sum by group and under some criteria Hi, Thanks! I need to do some c

Re: [R] Merging data in arrays

2013-02-07 Thread arun
Hi, I didn't fully understand the logic. You could get the result by:  list1<-lapply(mapply(cbind,lapply(1:2,function(i) E[,,i]),lapply(c(1,3),function(i) C[,i,]),SIMPLIFY=FALSE),function(x) x[,c(TRUE,apply(matrix(!x[,-1]%in% x[,1],nrow=5),2,all))]) Fnew<-array(unlist(list1),dim=c(dim(list1[[1]]

Re: [R] why "object 'x' not found"?

2013-02-07 Thread arun
Hi, You could get the result: dat1 <- read.table(text = " v1 v2 v3  1   0   0  0   1   0  0   0   1 ",sep="",header=TRUE) dat1$v4<-apply(mapply(`==`,dat1,1),2,which)  dat1  # v1 v2 v3 v4 #1  1  0  0  1 #2  0  1  0  2 #3  0  0  1  3 A.K. - Original Message - From: Winfried Moser To: r-

Re: [R] combining dataframes into single df with all columns

2013-02-07 Thread arun
Hi, You could use ?merge() or ?join() from library(plyr)  merge(df1,df2,all=TRUE) #  col2 col3 col6 col8 col9 col1 #1    2    3    6   NA   NA    1 #2   NA   NA   NA   NA   NA   NA A.K. - Original Message - From: Anika Masters To: r-help@r-project.org Cc: Sent: Thursday, February 7, 2

Re: [R] convert 12 time stamp to 24 hour

2013-02-08 Thread arun
Hi, library(lubridate)  ymd_hms(testtime)  1 parsed with %Y-%m-%d %I:%M:%S %p #[1] "2013-01-01 13:00:01 UTC" A.K. - Original Message - From: e-letter To: r-help@r-project.org Cc: Sent: Friday, February 8, 2013 4:44 AM Subject: [R] convert 12 time stamp to 24 hour Readers, For a 12 h

Re: [R] Merging and Updating Data Frames with Unequal Size

2013-02-08 Thread arun
Hi, Try:  R[,2:4]<-F[,2:4][match(R$x,F$x),]  R$p<- unique(F$p)     R     x y z  w k  p 1  -1 1 3  8 9 18 2  -2 2 4  9 9 18 3  -3 3 5 10 9 18 4  -1 1 3  8 9 18 5  -2 2 4  9 9 18 6  -3 3 5 10 9 18 7  -1 1 3  8 9 18 8  -2 2 4  9 9 18 9  -3 3 5 10 9 18 10 -1 1 3  8 9 18 11 -2 2 4  9 9 18 12 -3 3 5 10

Re: [R] help with creating new variables using a loop

2013-02-09 Thread arun
Hi,  mood<- data.frame(Mood1=1:5,Mood2=6:10,Mood3=11:15) lapply(seq_along(mood),function(i) i*mood[i]) A.K. - Original Message - From: christel lacaze To: r-help@r-project.org Cc: Sent: Thursday, February 7, 2013 11:16 AM Subject: [R] help with creating new variables using a loop Hi

Re: [R] help with creating new variables using a loop

2013-02-09 Thread arun
Hi, Just to add: If the columns are  not in the order (Mood1:Mood10) in the dataset.  mood<- data.frame(Mood2=6:10,Mood1=1:5,Mood10=11:15,Mood3=16:20)  do.call(cbind,lapply(names(mood),function(i) mood[i]*as.numeric(gsub("\\D+","",i #  Mood2 Mood1 Mood10 Mood3 #1    12     1    110    48 #2

Re: [R] top 10 (n values) for each classes

2013-02-10 Thread arun
You should have used dput(). Assuming the "," are "." #Using a subset of your example dat1<-read.table(text=" proc cls 7271 568,03338 0,5   7270 554,68458 0,5   7269 510,20638 0,5   7268 485,59969 0,5   7267 421,92852 0,5   7272 410,12101 0,5   3414 409,71429 0,5   3452 402,78699 0,5   3451 401,28

Re: [R] FORMAT EDITING

2013-02-11 Thread arun
Hi Elisa, It seems also that there are spaces between the second '.' and the number (1901.11. 1). You could do something like: Lines1<-readLines(textConnection("1901.11. 1 447.000    1901.11. 2 445.000    1901.11. 3 445.000    1924.11. 4 445.000    1924.11. 5 449.000    1924.11. 6 442.000

Re: [R] Changing the order of months within a year

2013-02-12 Thread arun
Hi, You could try: dat1<- read.table(text="     site  date year precipitation temp_max temp_min 1  castlepeak Jan-71 1971    26.2903226 38.29032 18.06452 2  castlepeak Feb-71 1971    9.1071429 39.60714 17.5 3  castlepeak Mar-71 1971    36.3548387 38.87097 17.77419 4  castlepeak Apr

Re: [R] subsetting data file by intoducing a second file

2013-02-12 Thread arun
Hi, bg<- read.table(text=" Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 Gi20Jun11  0.001217    0 0.001217    0 0.00    0    0    0 0.001217    0    0 

Re: [R] reorganize data

2013-02-12 Thread arun
Hi, Lines1 <- readLines(textConnection("Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32 -0010 |   28|   28 0010-0020|  302|  302 0020-0030|   42|   42 0030-0040|    2|    2 0040-0050|    1|    1 0060-0070|    1|    1 _ Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07

Re: [R] grabbing from elements of a list without a loop

2013-02-12 Thread arun
Hi,  mapply(`[`,mylist,list(1,2,0),SIMPLIFY=FALSE) #[[1]] #  a #1 1 #2 2 #[[2]]  # b #1 5 #2 6 #[[3]] #data frame with 0 columns and 2 rows A.K. - Original Message - From: Dimitri Liakhovitski To: r-help Cc: Sent: Tuesday, February 12, 2013 4:33 PM Subject: [R] grabbing from eleme

Re: [R] reorganize data

2013-02-12 Thread arun
Hi, Sorry, in my previous reply, I omitted one of the columns. Lines1 <- readLines(textConnection("Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32 -0010 |   28|   28 0010-0020|  302|  302 0020-0030|   42|   42 0030-0040|    2|    2 0040-0050|    1|    1 0060-0070|    1|    1 _ Date

Re: [R] grabbing from elements of a list without a loop

2013-02-12 Thread arun
Hi Dimitri,  neededcolumns<- c(1,2,0) mapply(`[`,mylist,lapply(neededcolumns,function(x) x),SIMPLIFY=FALSE) #[[1]]  # a #1 1 #2 2 # #[[2]]  # b #1 5 #2 6 #[[3]] #data frame with 0 columns and 2 rows A.K. From: Dimitri Liakhovitski To: arun Cc: R h

Re: [R] Max value of each 2 rows in dataframe

2013-02-12 Thread arun
Hi, For the dataset A, Assuming that this is what you meant: the maximum value of "lig" in each 2 rows excluding the first row: #dataset: A A[-1,]<-do.call(rbind,lapply(split(A[-1,],((1:nrow(A[-1,])-1)%/%2)+1),function(x){x[4]<-max(x[4]);x}))  A #   ok    time secs lig  geo  

Re: [R] Correlation with p value

2013-02-13 Thread arun
Hi, #data: dat1 mat1<-matrix(dat1[,2],ncol=3,nrow=7) colnames(mat1)<-unique(dat1[,1]) library(Hmisc) rcorr(mat1)$P  #     TTK   CTJ   PKR #TTK    NA 0.0151874 0.6277163 #CTJ 0.0151874    NA 0.5214368 #PKR 0.6277163 0.5214368    NA A.K. - Original Message - From:

Re: [R] date and matrices

2013-02-13 Thread arun
r(res1) #'data.frame':    2192 obs. of  2 variables: # $ x : Date, format: "1991-01-01" "1991-01-02" ... # $ V2: num  0.314 0.314 0.273 0.273 0.236 0.236 0.236 0.236 0.273 0.314 ... A.K. From: eliza botto To: "smartpink...@yaho

Re: [R] date and matrices

2013-02-13 Thread arun
uot; #[2190,] "1996.12.29" "0.236" #[2191,] "1996.12.30" "0.236" #[2192,] "1996.12.31" "0.236" A.K. From: eliza botto To: "smartpink...@yahoo.com" Sent: Wednesday, February 13, 2013 9:01 AM S

Re: [R] date and matrices

2013-02-13 Thread arun
0.236 0.264 0.236 0.295 0.315 ...  head(res3[[1]]) #   date1   val #1 1991.01.01 0.314 #2 1991.01.02 0.314 #3 1991.01.03 0.273 #4 1991.01.04 0.273 #5 1991.01.05 0.236 #6 1991.01.06 0.236 I am not sure whether you could do it in one step as memory might be an issue here.  You could split this

Re: [R] date and matrices

2013-02-13 Thread arun
ames")=List of 2   .. ..$ : NULL   .. ..$ : chr [1:2] "date1" "val"  # both are now of class "chr" A.K. From: eliza botto To: "smartpink...@yahoo.com" Sent: Wednesday, February 13, 2013 11:44 AM Subject: RE: date

Re: [R] seasonal sum and mean and combine multiple, different data frames in .csv

2013-02-13 Thread arun
2001  2002  2003  2004 #18055.442 20063.238 21481.157 20138.238  3141.716 In this dataset, some year doesn't have all the Seasons, so the value is less. I hope it hleps. A.K. - Original Message - From: "iruc...@mail2world.com" To: smartpink...@yahoo.com

Re: [R] list of matrices --> array

2013-02-13 Thread arun
Hi, May be this helps: library(plyr) res<-aaply(laply(my_list, as.matrix),c(2,3),function(x) x) attr(res,"dimnames")<- NULL  identical(res,foo) #[1] TRUE A.K. - Original Message - From: Murat Tasan To: r-help@r-project.org Cc: Sent: Thursday, February 14, 2013 1:13 AM Subject: Re: [R

Re: [R] spearman correlation and p-value as a matrix

2013-02-13 Thread arun
HI, #ag data was created bg<- as.matrix(read.table(text=" Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 Gi20Jun11  0.001217    0 0.001217    0 0.00    0    0 

Re: [R] spearman correlation and p-value as a matrix

2013-02-14 Thread arun
- colnames(res2)  identical(res1,res2) #[1] TRUE A.K. - Original Message - From: Ozgul Inceoglu To: arun Cc: Sent: Thursday, February 14, 2013 6:17 AM Subject: re:Re: [R] spearman correlation and p-value as a matrix Dear Arun, I bothered you this week alot, but it seems like that y

Re: [R] Looking for an easy way to change the names of a vector

2013-02-14 Thread arun
Hi, names(Data)[-grep(".*\\s+.*",names(Data))]<-paste(names(Data)[-grep(".*\\s+.*",names(Data))],"-XXX",sep="") names(Data)[grep(".*\\s+.*",names(Data))]<-gsub("[ ]","-",names(Data)[grep(".*\\s+.*",names(Data))])  names(Data) #[1] "ada-XXX"    "fsdfs-XXX"  "asd-fd" "asdsa-XXX"  "dsada-XXX"

Re: [R] spearman correlation and p-value as a matrix

2013-02-14 Thread arun
Dear Ozgul, I made a comment in the previous email, which I didn't check it. Thank you for the ?cor2m(). I checked the results of the example dataset with cor2m().  Does it have an option for spearman correlation?  The values below seem to be pearson #cor2m  cor2m(bg,ag,trim=TRUE,alpha=0.05) ##

Re: [R] spearman correlation and p-value as a matrix

2013-02-14 Thread arun
  NaN  NaN  NaN  NaN  NaN -0.8901029 #4  NaN  0.000  NaN  NaN  NaN  NaN  NaN  0.000 #5  NaN  0.000  NaN  NaN  NaN  NaN  NaN  0.000  # Otu00209   Otu00218 #1  NaN  0.000 #2  NaN  0.000 #3      NaN -0.8901029

Re: [R] match in dependence of 2 columns

2013-02-14 Thread arun
HI, If the dataset is similar to this: dat1<- read.table(text="   cu.nr. name  value  A 1 Evo 100 B 1 Mer 80  C 2 Ford

Re: [R] appending data to a row

2013-02-14 Thread arun
HI, May be this helps: (assuming that there are only twins) dat1<- read.table(text=" FamilyID  ParticipantID  IQ  Digit_span 1    1  95  6 1    2  93  7 2    3  102 8 2    4  101 9 3    5  106 7 3    6 

Re: [R] Format Integers

2013-02-14 Thread arun
Hi, Try vec1<- c(10, 1000153846, 1000307692, 1000461538, 1000615385) round(vec1/1e6,2) #[1] 1000.00 1000.15 1000.31 1000.46 1000.62 #or as.numeric(sprintf("%.2f",vec1/1e6)) #[1] 1000.00 1000.15 1000.31 1000.46 1000.62 A.K. - Original Message - From: Alaios To: R help Cc: S

Re: [R] Using paste on results from tapply?

2013-02-14 Thread arun
HI, You could either get the results by: unlist(lapply(groups,function(x) paste(x,collapse=" ")),use.names=FALSE) #[1] "6" "4 4"   "5 3 2" #or gsub("[c(),]","",paste(groups)) #[1] "6" "4 4"   "5 3 2" str(gsub("[c(),]","",paste(groups))) # chr [1:3] "6" "4 4" "5 3 2"  str(unlist(lapply(gro

Re: [R] Using paste on results from tapply?

2013-02-15 Thread arun
HI, You could also use these: sapply(groups,paste,collapse=" ") #  1   3   6  #   "6"   "4 4" "5 3 2" #or gsub("^ | $","",gsub("\\D+"," ",paste(groups))) #[1] "6" "4 4"   "5 3 2" A.K. - Original Message - From: Nikola Janevski To: r-help@r-project.org Cc: Sent: Thu

Re: [R] match a task No. to a few person-IDs

2013-02-15 Thread arun
Hi, Try this: dat1<- read.table(text="   Task_No  Team   A    49397    1 B    49396    1 ",sep="",header=TRUE) dat2<- read.table(text="     ID    Team A 5019    1 B 472    1 C 140    1 D 5013    1 ",sep="",header=TRUE)  res1<- as.matrix(merge(dat1,dat2,by="Team"))  ro

<    2   3   4   5   6   7   8   9   10   11   >