Re: [R] scanning data in R

2013-04-03 Thread arun
vec<- scan("stop.txt") #Read 635 items vec1<-vec[vec!=21]  length(vec1) #[1] 584 any(vec1==21) #[1] FALSE A.K. - Original Message - From: Naser Jamil To: R help Cc: Sent: Wednesday, April 3, 2013 5:33 AM Subject: [R] scanning data in R Dear R-user, May I seek your suggestion.  I have

Re: [R] Creating data frame from individual files

2013-04-03 Thread arun
Hi, suppose, you have 3 files with 2 columns: named.list<- list(structure(list(col1 = 1:6, col2 = c(0.5, 0.2, 0.3, 0.3, 0.1, 0.2)), .Names = c("col1", "col2"), class = "data.frame", row.names = c(NA, -6L)), structure(list(col1 = 1:6, col2 = c(0.9, 0.7, 0.5, 0.2, 0.5, 0.2)), .Names = c("col1", "

Re: [R] Superscript

2013-04-04 Thread arun
Hi, DATA_names<-c("A_mgkg","B_mgkg","C_mgkg","D_mgkg","E_mgkg","F_mgkg","G_mgkg","H mgkg")  lapply(DATA_names,function(x) if(grepl("_",x)) f(x) else x)[1:2] #[[1]] #A ~ (mg ~ kg^{ #    -1 #}) #[[2]] #B ~ (mg ~ kg^{  #   -1 #})  f("Na_mgkg") #Na ~ (mg ~ kg^{ #    -1 #}) A.K. - Original Me

Re: [R] summing vectors

2013-04-04 Thread arun
HI, I assume that you have a data.frame structure. dat1<- structure(list(Year = c(2000L, 2000L, 2000L, 2000L, 2001L, 2001L, 2001L, 2001L, 2001L), Area = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Q = c(1L, 1L, 1L, 1L, 25L, 1L, 1L, 1L, 1L), Bin = c(5L, 10L, 15L, 20L, 1L, 5L, 10L, 15L, 20L), FD = c(0

Re: [R] summing vectors

2013-04-04 Thread arun
You could also use: library(data.table) dat2<- data.table(dat1) dat2<-dat2[,setdiff(colnames(dat2),"Q"),with=FALSE]  dat2[,lapply(.SD,sum),by=c("Year","Area")] #   Year Area Bin FD #1: 2000    1  50 36 #2: 2001    1   1  4 #3: 2001    2  50 23 A.K. ----- O

Re: [R] summing vectors

2013-04-04 Thread arun
value.var="FD",sum)   names(res)[grep("^\\d+",names(res))]<-paste0("FD_",names(res)[grep("^\\d+",names(res))])  res #  Bin FD_1_2000 FD_2_2000 FD_2_2001 #1   5 4    16 0 #2  10 2     8 8 #3  15    26    12

Re: [R] Creating data frame from individual files

2013-04-04 Thread arun
lt;- names(lst1)  res #  File1 File2 #A   2.4   1.3 #B   2.1   0.4 A.K. From: Adrian Johnson To: arun Sent: Thursday, April 4, 2013 2:49 PM Subject: Re: [R] Creating data frame from individual files HI: sorry for confusion.  I have 72 files and all 72 files

Re: [R] about subsetting vectors/list in R

2013-04-04 Thread arun
Hi, You could also use: library(seqinr) x1<- rep(c('A','G','C','T'),4) splitseq(x1,frame=0,word=2) #[1] "AG" "CT" "AG" "CT" "AG" "CT" "AG" "CT" splitseq(x1,frame=1,word=2) #[1] "GC" "TA" "GC" "TA" "GC" "TA" "GC"  splitseq(x1,frame=0,word=3) #[1] "AGC" "TAG" "CTA" "GCT" "AGC" A.K. - Orig

Re: [R] Better way of writing R code

2013-04-05 Thread arun
Hi, You can preserve the order using join().  Though, in this case, merge would be easier. library(plyr) both<- join(currency_df,rate_df,type="left") names(both)[14:15]<- paste0("currency_",names(both)[14:15]) names(rate_df)[2]<- "other_currency" both2<- join(both,rate_df,type="left",by=c("other

Re: [R] Extract lines from file

2013-04-05 Thread arun
HI, " btw, could this program be modified such that it take direct input from my input.txt file???"  Lines1<-readLines("utpalmtbl.txt") Lines1 #[1] "OG1: or10|1345 or10|387 or10|474 or11|1203 or11|182 or10|2158 or12|637" #[2] "OG2: or10|1562 or10|1584 or10|1977 or11|2263 or11|43"   

Re: [R] Extract lines from file

2013-04-05 Thread arun
Hi Utpal, You can use the same script from my previous email.   Lines1<- readLines("groups.txt") library(stringr) res<-paste(gsub("(.*\\:).*","\\1",Lines1),unlist(lapply(str_match_all(Lines1,"or10\\|\\d+"),paste,collapse=" ")),sep=" ") write.table(res,"res1.txt",row.names=FALSE,col.names=FALSE,

Re: [R] How to perform a grouped shapiro wilk test on dataframe

2013-04-05 Thread arun
Hi, Try this: dat1<- read.csv("sample.csv",sep="\t",stringsAsFactors=FALSE)  with(dat1,tapply(COUNTS,list(ACTIVITY),function(x) if (length(unique(x))==1) NA else shapiro.test(x))) #$activity1 #[1] NA #$activity2 #    Shapiro-Wilk normality test #data:  x #W = 0.4588, p-value = 8.025e-11 # #$ac

Re: [R] How to perform a grouped shapiro wilk test on dataframe

2013-04-05 Thread arun
88439 #3 activity3 3.760396e-07 0.7282838 A.K. - Original Message - From: "Mossadegh, Ramine N." To: arun Cc: Sent: Friday, April 5, 2013 4:17 PM Subject: RE: [R] How to perform a grouped shapiro wilk test on dataframe The statistic & the p.value .  When I

Re: [R] manipulating R contingency tables

2013-04-06 Thread arun
Hi, Try this: tbl1<- structure(c(21L, 23L, 127L, 112L, 120L, 0L), .Dim = 2:3, .Dimnames = structure(list(     labels = c(1, 2), gts  = c("A1", "B2", "G3")), .Names = c("labels", "gts")), class = "table") dat1<-as.data.frame(tbl1,stringsAsFactors=FALSE) dat2<-dat1[dat1$gts!="B2" & dat1$Freq!=0,]

Re: [R] arrange data

2013-04-06 Thread arun
Hi, Try this: #dat1 is dataset indx<-apply(dat1,2,function(x) head(which(!is.na(x)),2)) res<-as.data.frame(sapply(seq_len(ncol(indx)),function(i) dat2[indx[,i],i]))  colnames(res)<- colnames(dat1)  res # 1B    2B    4B    1A    2A    4A    5B    5A  C31A  C31B  C34A  C34B  C35A #1 2.518 2.357

Re: [R] arrange data

2013-04-06 Thread arun
ted to have the same number of rows as dat1: nrow(dat1) #[1] 145 new1<-as.data.frame(matrix(NA,ncol=23,nrow=5))  colnames(new1)<- colnames(res)  res1<- rbind(res,new1)  nrow(res1) #[1] 145 A.K. From: catalin roibu To: arun Sent: Saturday, April 6

Re: [R] Replace missing value within group with non-missing value

2013-04-06 Thread arun
6 487 A.K. - Original Message - From: "Leask, Graham" To: Rui Barradas Cc: arun ; "r-help@r-project.org" Sent: Saturday, April 6, 2013 1:44 PM Subject: RE: [R] Replace missing value within group with non-missing value Hi Rui, I have just pasted this direct and reru

Re: [R] arrange data

2013-04-06 Thread arun
ot;  "58"  "59"  "60"  [61] "61"  "62"  "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71"  "72"  [73] "73"  "74"  "75"

Re: [R] manipulating R contingency tables

2013-04-06 Thread arun
g a specific column.  tbl1[,!grepl("B2",colnames(tbl1)) & colSums(tbl1==0)==0] # 1  2 #21 23  tbl1[,colSums(tbl1==0)==0] #  gts #labels A1  B2  #    1 21 127   #   2 23 112 A.K. - Original Message - From: arun To: Abhishek Pratap Cc: R help Sent: Saturday, Apri

Re: [R] list of distance matrices

2013-04-06 Thread arun
Hi, Try this: set.seed(25)  el<-as.data.frame(matrix(sample(1:50,75*124,replace=TRUE),ncol=75))  lst1<-lapply(el,function(x) dist(x))  set.seed(28)  q<-as.data.frame(matrix(sample(1:50,75*124,replace=TRUE),ncol=75))  q<-dist(q) library(ade4) res<- lapply(lst1,function(x) mantel.rtest(q,x,nrepet=999

Re: [R] Mutliple subsetting of a dataframe based on many conditions

2013-04-06 Thread arun
Hi, May be this helps: input1<- data.frame(answer=rep(1:4,times=18),p.number=rep(1:18,each=4),session=rep(1:2,each=36),count=rep(1:8,each=9),type=rep(1:3,each=24)) input2<- data.frame(answer=rep(1:4,times=18),p.number=rep(1:18,each=4),session=rep(1:2,each=36),count=rep(1:8,each=9),type=rep(1:3,e

Re: [R] mlogit error

2013-04-07 Thread arun
local guidelines/advice from PCT London 3 - 5 PARTNERS  32 #    ALL_P sex disp form set3 set set2 tag1 mode.ids indivs CHOICE #1.1 0.5295166   1    0    0    1   1    1    4    1  1  FALSE #1.2 0.5295166   1    0    0    1   1    1    4    2  1  FALSE A.K. - Original Message

Re: [R] group data in classes

2013-04-07 Thread arun
Hi, Try: year1<- 1598:1997  indx<-findInterval(year1,seq(1591,2000,by=10)) group<-seq(1590,2000,by=10) ind<-seq(1,length(group),by=1) labl1<-paste(group[ind],group[ind+1],sep="-")[-42] dat1<- data.frame(year=year1,decade=labl1[indx],stringsAsFactors=FALSE) head(dat1,5) #  year    decade #1 1598 1

Re: [R] Remove a row containing a specific value for a column

2013-04-07 Thread arun
Hi, DATA[DATA$A!="Blue1",] # A B C D #1 Red1 1 1 1 #3 Red2 1 1 1 #4 Red3 1 1 1 #or DATA[!grepl("Blue1",DATA$A),] # A B C D #1 Red1 1 1 1 #3 Red2 1 1 1 #4 Red3 1 1 1 A.K. - Original Message - From: Beatriz González Domínguez To: r-help@r-project.org; R Help Cc: Sent: Sunday, A

Re: [R] mlogit error

2013-04-07 Thread arun
Hi, Try this:  Mult3$mode.ids  #[1] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 #Levels: 1 2 3 4 5 6 I guess the key is to have equal number of alternatives.  Mult3$mode.ids<- as.numeric(as.character(Mult3$mode.ids))   res<-mlogit.data(Mult3[-25,],shape="long",choice="CHOICE",alt.var="mode.id

Re: [R] Reshaping a table

2013-04-08 Thread arun
Hi, Try this: dat1<-read.table(text=" X  Y1    Y2 0.1  3  2 0.2  2  1 ",sep="",header=TRUE)  res<-do.call(rbind,lapply(split(dat1,seq_len(nrow(dat1))),function(x) {Y=rep(colnames(x)[-1],x[-1]); X=rep(x[,1],length(Y)); data.frame(X,Y,stringsAsFactors=FALSE)}))  row.names(res)<- 1:n

[R] How to replace zero with the character value- need help

2013-04-08 Thread arun
Hi, Not sure if you have only one "country" or not. Try this: dat<- data.frame(val,state,country,stringsAsFactors=FALSE) dat$country[dat$country==0]<-dat$country[1] #or #dat$country[dat$country==0]<- dat$country[dat$country!=0]   res<-do.call(rbind,lapply(split(dat,cumsum(grepl("[A-Za-z]",dat$

Re: [R] Reshaping a table

2013-04-08 Thread arun
","integer")) as.table(df1) #Error in as.table.default(df1) : cannot coerce to a table I am not sure about the problem. A.K. - Original Message - From: Bert Gunter To: arun Cc: "dcarl...@tamu.edu" Sent: Monday, April 8, 2013 1:33 PM Subject: Re: [R] Reshaping a

Re: [R] How can I extract part of the data in a panel dataset?

2013-04-08 Thread arun
Hi, 1) Grunfeld1951<-Grunfeld[Grunfeld$year==1951,]  Grunfeld1951 #    firm year    inv   value capital #17 1 1951 755.90 4833.00 1207.70 #37 2 1951 588.20 2289.50  342.10 #57 3 1951 135.20 1819.40  671.30 #77 4 1951 160.62  809.00  203.50 #97 5 1951  80.30  327.30  683.90 #117 

Re: [R] How can I extract part of the data in a panel dataset?

2013-04-08 Thread arun
  748.1000 #6 6  206.8500 #7 7  478.7500 #8 8  165.8750 #9 9  429. #10   10   11.2525 A.K. - Original Message - From: arun To: jpm miao Cc: R help Sent: Monday, April 8, 2013 2:04 PM Subject: Re: [R] How can I extract part of the data in a panel dataset? Hi, 1

Re: [R] remove duplicates in data frame

2013-04-08 Thread arun
Hi, Try ?unique() You haven't provided reproducible data or information about the package.  So, this may or may not work. dat1<- data.frame(idvar=c(rep(1,2),2,4),col2=c(7,7,8,9))  dat1 #  idvar col2 #1 1    7 #2 1    7 #3 2    8 #4 4    9  unique(dat1) #  idvar col2 #1 1    7

Re: [R] (no subject)

2013-04-08 Thread arun
Hi, Try this: set.seed(28)   t1<-data.frame(id=rep(1:3,rep(3,3)),dt=rep(1:3,rep(9,3)),var=c('num1','num2','norm'),value=rnorm(27))  head(t1) #  id dt  var   value #1  1  1 num1 -1.90215722 #2  1  1 num2 -0.06429479 #3  1  1 norm -1.33116707 #4  2  1 num1 -1.81999167 #5  2  1 num2  0.16266969 #6

Re: [R] (no subject)

2013-04-08 Thread arun
2  1 num2  0.30611744 #5  3  1 num1 -0.94940980 #6  3  1 num2 -0.06622304 identical(res,dfNew) #[1] TRUE A.K. - Original Message - From: arun To: neal subscribe Cc: R help Sent: Tuesday, April 9, 2013 12:59 AM Subject: Re: [R] (no subject) Hi, Try this: set.seed(28)   t1<-data.fram

Re: [R] Reading Data

2013-04-08 Thread arun
Hi, I tried to read your data from the image: OPENCUT<- read.table("OpenCut.dat",header=TRUE,sep="\t") OPENCUT   FC LC  SR  DM 1  400030.34 1323.5   0 400 2   12680.13    2.5   0 180 3  472272.75 2004.7   3 300 4  332978.03 1301.3 106 180 5   98654.20  295.0   0 180 6   68142.05  259.9

Re: [R] quotes in cat() within function

2013-04-09 Thread arun
file<- "\"Data labels\""  directory="\"/home/mylaptop/\""  cat("The file", file,"is located in directory",directory,sep=" ") The file "Data labels" is located in directory "/home/mylaptop/" A.K. - Original Message - From: Daniel Caro To: r-help@r-project.org Cc: Sent: Tuesday, April 9,

Re: [R] Behaviors of diag() with character vector in R 3.0.0

2013-04-09 Thread arun
Hi, You could try this: v <- c("a", "b") mat1<-diag(length(v))  diag(mat1)<- v  mat1 # [,1] [,2] #[1,] "a"  "0" #[2,] "0"  "b"  v1<- letters[1:5] mat2<- diag(length(v1))  diag(mat2)<- v1 mat2 # [,1] [,2] [,3] [,4] [,5] #[1,] "a"  "0"  "0"  "0"  "0" #[2,] "0"  "b"  "0"  "0"  "0" #[3,]

Re: [R] rep() fails at times=0.29*100

2013-04-09 Thread arun
Possibly R FAQ 7.31 length(rep(TRUE,signif(0.29*100,2))) #[1] 29 A.K. - Original Message - From: Jorge Fernando Saraiva de Menezes To: r-help@r-project.org Cc: Sent: Tuesday, April 9, 2013 12:11 PM Subject: [R] rep() fails at times=0.29*100 Dear list, I have found an unusual behavior

Re: [R] R problem and help

2013-04-09 Thread arun
A NA NA NA NA NA NA NA NA #    17-234 18 19 20 23 24 25 26 27 HAR133 HAR105 HAR211 HUMH255 HUMH276 major #99  NA NA NA NA NA NA NA NA NA NA NA NA  NA  NA 0 #100 NA NA NA NA NA NA NA NA NA NA NA NA  NA  NA    NA #    minor #99  7 #100   

Re: [R] Converting matrix to data frame without losing an assigned dimname

2013-04-09 Thread arun
Hi, library(plyr) library(reshape2)  df2<-mutate(dcast(melt(tableData),State~Var2), State=factor(State,levels=rownames(tableData))) dfNew<-df2[as.numeric(df2$State),c(1,3:4,2)] dfNew$State<- as.character(dfNew$State)  dfNew # State Frost Population  Area #1  Connecticut   139   3100 

Re: [R] How to replace zero with the character value- need help

2013-04-10 Thread arun
Hi, "  >Thank You, Arun Kirshna , for your efforts and time. >I also found an equivalent code:  >library(zoo) >dat[] <- lapply(dat, function(x){replace(x, x == 0, NA)}) >dat <-na.locf(dat) " No problem. If you have equal replications, you could also us

Re: [R] Stringr Package

2013-04-10 Thread arun
Hi, Assuming that this is a data.frame. dat1<-read.table(text=" X,Y ab,su-di ac,pi-tu ad,tu-tu ",sep=",",header=TRUE,stringsAsFactors=FALSE) library(stringr) dat2<-data.frame(X=rep(dat1$X,each=2),Y= unlist(str_split(dat1$Y,"-")),stringsAsFactors=FALSE)  dat2 #   X  Y #1 ab su #2 ab di #3 ac pi #4

Re: [R] Stringr Package

2013-04-10 Thread arun
quot;-")),side="both")   X<-unlist(str_split(str_trim(str_dup(str_pad(dat1$X,width=3,side="right"),2))," ")) data.frame(X,Y,stringsAsFactors=FALSE)  #  X  Y #1 ab su #2 ab di #3 ac pi #4 ac tu #5 ad tu #6 ad tu A.K. - Original Message - From: arun

Re: [R] Using PLYR to apply a custom function to a data frame

2013-04-10 Thread arun
Hi, You could try ?within() set.seed(25) ALL<- data.frame(q3=sample(15:30,10,replace=TRUE), q1=sample(1:14,10,replace=TRUE)) res<-within(ALL,{lower<-q1- 1.5*(q3-q1);upper<-q3+1.5*(q3-q1)})  res #   q3 q1 upper lower #1  21  5  45.0 -19.0 #2  26  6  56.0 -24.0 #3  17 14  21.5   9.5 #4  29  9  59.

Re: [R] how to calculate average of each column

2013-04-10 Thread arun
Hi, TRy this: set.seed(52) dat1<- as.data.frame(matrix(sample(1:40,100*60,replace=TRUE), nrow=600)) lapply(split(dat1,((seq_len(nrow(dat1))-1)%/% 60)+1),nrow) #$`1` #[1] 60 #$`2` #[1] 60 #$`3` #[1] 60 res<-lapply(split(dat1,((seq_len(nrow(dat1))-1)%/% 60)+1),colMeans)  res[1:2] #$`1` #  V1 

Re: [R] means in tables

2013-04-10 Thread arun
Hi, This could be done in different ways: tab1<-read.table(text=" V1  V2  V3  V4  V5 14.23 1.71 2.43 15.6 127 13.20 1.78 2.14 11.2 100 13.16 2.36 2.67 18.6 101 14.37 1.95 2.50 16.8 113 13.24 2.59 2.87 21.0 118 ",sep="",header=TRUE) tab2<-read.table(text=" V1  V2  V3  V4  V5 1.23 1.1 2.3 1.6 17 1.20

Re: [R] Replace NAs in a data frame with elements randomly generated from the beta distribution

2013-04-10 Thread arun
Hi, Try this: which(MAT==0)  #[1] 10 19 22 27 28 34 40 46 51 52 MAT[MAT==0]<-sapply(seq_along(MAT[MAT==0]),function(x)rbeta(1,0.1,1)) MAT #    X1   X2  X3  X4   X5   X6 #1 0.68 9.731207e-14  0.03005021 0.003249449 1.36e+00 1.180602e-03 #2 9.86 1.15e

Re: [R] how to calculate average of each column

2013-04-10 Thread arun
: arun Cc: R help Sent: Wednesday, April 10, 2013 4:50 PM Subject: Re: [R] how to calculate average of each column Thanks so much! it works fine! But when I convert the result to a data frame, it mess up. so i have: res <- data.frame(res) then i get: X1 X2 X3

Re: [R] grup function

2013-04-10 Thread arun
Hi, The pattern is not clear.  In this particular case, library(stringr) word1<-c("Shangh i", "Hello here i am","h llo")  word1[str_count(word1," ")==1]<-gsub(" ","a",word1[str_count(word1," ")==1])  word1 #[1] "Shanghai"    "Hello here i am" "hallo" But, this will  not work here: word2<-c(

Re: [R] how to calculate average of each column

2013-04-10 Thread arun
.7 22.40678 #6  21.86207 21.3 #7  20.81034 17.25000 #8  21.5 21.45763 #9  22.18966 19.7 #10 22.31579 20.58929 A.K. From: Ye Lin To: arun Sent: Wednesday, April 10, 2013 6:02 PM Subject: Re: [R] how to calculate average of each column Hey A.

Re: [R] means in tables

2013-04-10 Thread arun
.72  mean(unlist(lapply(lst1,function(x) x[5,2]))) #[1] 20.97 A.K. - Original Message - From: Silvano Cesar da Costa To: arun Cc: Sent: Wednesday, April 10, 2013 6:02 PM Subject: Re: [R] means in tables Hi Arun, I thought with an example with two tables I could generalize to the 100 tab

Re: [R] means in tables

2013-04-10 Thread arun
1]] #  col1 col2 #1    1  0.5 #2    2  0.2 #3    3  0.3 #4    4  0.3 #5    5  0.1 #6    6  0.2 library(abind) apply(abind(lst2,along=3),c(1,2),mean) # col1  col2 #[1,]    3 0.500 #[2,]    4 0.467 #[3,]    5 0.567 #[4,]    6 0.267 #[5,]    7 0.400 #[6,]    8 0.267 A

Re: [R] means in tables

2013-04-10 Thread arun
0.012   0.373 dim(res) #[1] 8000    3 A.K. - Original Message - From: Silvano Cesar da Costa To: arun Cc: Sent: Wednesday, April 10, 2013 9:38 PM Subject: Re: [R] means in tables I performed all the procedures you described. I read each file with the command: lst2[[1]]; lst2[[2]]

Re: [R] means in tables

2013-04-10 Thread arun
HI Silvano, No problem. Just wanted to make sure that it worked. Regards, A.K. - Original Message - From: Silvano Cesar da Costa To: arun Cc: Sent: Wednesday, April 10, 2013 10:34 PM Subject: Re: [R] means in tables Arun, this code work very well: setwd('c:/Dados/')

Re: [R] means in tables

2013-04-10 Thread arun
,sep=""),collapse="+")))/length(lst1)) # user  system elapsed  # 0.060   0.000   0.063 #faster system.time(res<-apply(abind(lst1,along=3),c(1,2),mean)) #   user  system elapsed  # 0.356   0.024   0.380    res<- as.data.frame(res) identical(res,res1) #[1] TRUE A.K. - Ori

Re: [R] (no subject)

2013-04-11 Thread arun
HI, May be this helps: library(Unicode) utf2uxxx<- function(x){  a<- tolower(as.u_char(utf8ToInt(x)))  a<- gsub("[+]","",a)  a<- paste(paste("\\",a,sep=""),collapse="")  a<-cat('"',a,'"',sep="")} utf2uxxx("õäöü") #"\u00f5\u00e4\u00f6\u00fc" sessionInfo() R version 3.0.0 (2013-04-03) Platform: x86

Re: [R] group data

2013-04-11 Thread arun
Hi, dat1<- read.table(text=" ID  Value AL1  1 AL2  2 CA1  3 CA4  4 ",sep="",header=TRUE,stringsAsFactors=FALSE) dat2<- dat1 dat1$State<-gsub("\\d+","",dat1$ID) dat1 #   ID Value State #1 AL1 1    AL #2 AL2 2    AL #3 CA1 3    CA #4 CA4 4    CA #or library(stringr) dat2$State<-s

Re: [R] Read the data from a text file and reshape the data

2013-04-11 Thread arun
Hi, May be this helps:  lines1<- readLines("WAT_DEP.DAT.part") indx<- which(grepl("[*]",lines1)) indx2<-indx[seq(from=indx[2],length(indx),by=2)]+1 lines2<-str_trim(lines1[indx2],side="left") dat1<-read.table(text=lines2,sep="",header=FALSE) library(stringr) lst1<- lapply(split(indx,((seq_along(in

Re: [R] processing matrix equation derived from rows of two matrices

2013-04-11 Thread arun
Hi, May be this helps:  tb[1,]%*%(((val-rep(meansb79[1,],5))^2)/6) #    [,1] #[1,] 1.47619 tryvarb<-c(1,2,3,4,4,4,4)  var(tryvarb) #[1] 1.47619 tb[2,]%*%(((val-rep(meansb79[2,],5))^2)/6) # [,1] #[1,] 1.904762 sapply(seq_len(nrow(tb)),function(i) tb[i,]%*%(((val-rep(meansb79[i,],5))^

Re: [R] Adding unequal tables

2013-04-12 Thread arun
Hi, table.x<- as.table(as.matrix(read.table(text=" A    B C    D 2 4  6 5 ",sep="",header=TRUE))) table.y<- as.table(as.matrix(read.table(text=" C  D 5   1 ",sep="",header=TRUE))) library(plyr) library(reshape2) vec1<-rowSums(join(melt(table.x)[,-1], melt(table.y)[,-1],by="Var2")[

Re: [R] processing matrix equation derived from rows of two matrices

2013-04-12 Thread arun
hoo.com Cc: Sent: Friday, April 12, 2013 1:05 PM Subject: FW: [R] processing matrix equation derived from rows of two matrices Thanks so much Arun.  If you have time, what reference(s) do you use to learn the techniques like "seq_len(nrow" etc and other aspects of your code? Again. Th

Re: [R] how to change the date into an interval of date?

2013-04-12 Thread arun
Hi, I am not sure I understand your question correctly. dat1<- read.table(text=" id    responsed_at number_of_connection  scores 1  12-01-2010   1       2 1 

Re: [R] Removing rows that are duplicates but column values are in reversed order

2013-04-12 Thread arun
Hi, >From your example data, dat1<- read.table(text=" id1   id2   value a  b   10 c  d    11 b a 10 c  e 12 ",sep="",header=TRUE,stringsAsFactors=FALSE) #it is easier to get the output you wanted dat1[!duplicated(dat1$value),] #  id1 id2 value #1   a   b  

Re: [R] Search for common character strings within a column

2013-04-12 Thread arun
Hi, May be this helps: Not sure how you wanted to select those two letters.  dat1<- read.table(text="    Seq,Output  A B B C D A C,Yes  B C A C B D A C,Yes C D A A C D,No ",sep=",",header=TRUE,stringsAsFactors=FALSE) library(stringr) lapply(str_split(str_trim(dat1$Seq)," ")[dat1$Output=="Yes"],f

Re: [R] Search for common character strings within a column

2013-04-12 Thread arun
x[order(x)],collapse="")); x3<-table(x2); x3[x3%in% max(x3)]}) dat1$MaxCombn[dat1$Output=="Yes"]<-lapply(res1,names)  dat1 #   Seq Output   MaxCombn #1    A B B C D A C    Yes AB, AC, BC #2  B C A C B D A C    Yes AC, BC #3  C D A A C D No NA A

Re: [R] split date and time

2013-04-12 Thread arun
Hi, dat1<- read.table(text=" Number,TimeStamp,Value 1,1/1/2013 0:00,1 2,1/1/2013 0:01,2 3,1/1/2013 0:03,3 ",sep=",",header=TRUE,stringsAsFactors=FALSE) dat2<-data.frame(Number=dat1[,1],do.call(rbind,strsplit(dat1[,2]," ")), Value=dat1[,3])  names(dat2)[2:3]<- c("Date","Time")  dat2 #  Number D

[R] Fw: split date and time

2013-04-12 Thread arun
- Forwarded Message - From: arun To: Ye Lin Cc: Sent: Friday, April 12, 2013 6:25 PM Subject: Re: [R] split date and time Hi Ye, Is this okay? dat2<-cbind(dat1[,-2],do.call(rbind,strsplit(dat1[,2]," ")),stringsAsFactors=FALSE)  dat2 #  Number Value    1  

Re: [R] Comparison of Date format

2013-04-12 Thread arun
Hi,  In the example you provided, it looks like the dates in Date2 happens first.  So, I changed it a bit.  DataA<- read.table(text=" ID,Status,Date1,Date2            1,A,3-Feb-01,15-May-01         1,B,15-May-01,16-May-01         1,A,16-May-01,3-Sep-01                     1,B,3-Sep-01,

Re: [R] Comparison of Date format

2013-04-12 Thread arun
=0] library(plyr)  dataNew<-do.call(rbind,lapply(lst2,function(x) {colnames(x)[3]<- colnames(DataB)[2];x})) res<-join(dataNew,DataB,by=c("Date.Accident","ID"),type="right")  identical(res,res2) #[1] TRUE A.K. - Original Message - From: arun To: farnoos

Re: [R] Reshaping Data for bi-partite Network Analysis

2013-04-13 Thread arun
Hi, Try this; library(reshape2) res<-dcast(Input,people~place,value.var="time") res[is.na(res)]<-0  res #  people beach home school sport #1    Joe 5    3  0 1 #2   Marc 0    4  2 0 #3   Mary 0    0  4 0 #or  xtabs(time~.,Input) #  place #people beach home s

Re: [R] how to add a row vector in a dataframe

2013-04-13 Thread arun
Hi, Using S=1000 and simdata <- replicate(S, generate(3000)) #If you want both "m1" and "m0" #here the missing values are 0 res1<-sapply(seq_len(ncol(simdata.psm1)),function(i) {x1<-merge(simdata.psm0[,i],simdata.psm1[,i],all=TRUE); x1[is.na(x1)]<-0; x1}) res1[,997:1000] #  [,1] [,

Re: [R] Comparison of Date format

2013-04-13 Thread arun
te(DataA1$Date2,format="%d-%b-%y") DataB1<- DataB DataB1$Date.Accident<- as.Date(DataB1$Date.Accident,format="%d-%b-%y") dataNew2<-do.call(rbind,lapply(unique(DataA1$ID),function(i) { x1<- DataA1[DataA1$ID==i,] ;x1New<- t(x1); x2<-DataB1[DataB1$ID==i,]; do.ca

Re: [R] Reshaping Data for bi-partite Network Analysis [SOLVED]

2013-04-14 Thread arun
put) #   place #people school home sport beach  # Marc  2    4 0 0   #Joe   0    3 1 5   #Mary  4    0 0 0 A.K. From: sylvain willart To: arun Cc: R help Sent: Saturday, April 13, 2013 5:41 PM Subject: Re: [R] Reshaping Data for

Re: [R] Create New Column Inside Data Frame for Many Data Frames

2013-04-14 Thread arun
Hi,  lapply(LETTERS[1:2],function(x) {x1<-get(x); x1$Rate<- ROC(x1$population);x1}) #[[1]]  # population   Rate #1    100 NA #2    300  1.0986123 #3   5000  2.8134107 #4   2000 -0.9162907 #5    900 -0.7985077 #6   2500  1.0216512 #[[2]]  # population   Rate

Re: [R] possible loop problem

2013-04-14 Thread arun
Hi, It would be better if you provided the output of dput(dataset).  I am not sure about the structure of your dataset. Just from reading the data as is shown. dat1<- read.table(text=" separator,tissID >,>,2 ,2,1 ,6,5 ,11,13 >,>,4 ,4,9 ,6,2 ,7,3 ,21,1 ,23,58 ,25,9 ,26,4 >,>,11 ,1,12 >,>,21 ,4,1

Re: [R] Sorting data.frame and again sorting within data.frame

2013-04-15 Thread arun
library(plyr) arrange(df,names,desc(dates)) #  names dates values #1 A 4/15/2013 31 #2 A 4/14/2013    102 #3 A 4/13/2013 31 #4 B 4/15/2013 34 #5 B 4/14/2013 47 #6 B 4/13/2013 17 #7 C 4/15/2013 10 #8 C 4/14/2013 29 #9 C 4/13/2013   

Re: [R] Sorting data.frame and again sorting within data.frame

2013-04-15 Thread arun
/2013 29 #5 C 4/13/2013 11 A.K. - Original Message - From: arun To: Katherine Gobin Cc: R help Sent: Monday, April 15, 2013 8:57 AM Subject: Re: [R] Sorting data.frame and again sorting within data.frame library(plyr) arrange(df,names,desc(dates)) #  names dates values #1

Re: [R] Kruskal-Wallis

2013-04-15 Thread arun
Hi, set.seed(25)  myFile1<-as.data.frame(matrix(sample(1:40,50,replace=TRUE),nrow=10))  row.names(myFile1)<- LETTERS[1:10] groups <- rep (0:1, c(3,2)) kruskal<-apply(myFile1,1,kruskal.test,groups)  p_kruskal <- sapply(kruskal, function(x) x$p.value)  p_kruskal # A  B  C

Re: [R] how to transform string to "Camel Case"?

2013-04-15 Thread arun
HI, You could use: gsub("(^|\\s+)([a-z])","\\1\\U\\2",z,perl=TRUE) #[1] "R Project"  "Hello World"    "Something Else" #or  gsub("\\b([a-z])","\\U\\1",z,perl=TRUE) #[1] "R Project"  "Hello World"    "Something Else" A.K. - Original Message - From: Liviu Andronic To: r-help Cc:

Re: [R] Indices of lowest values in matrix

2013-04-15 Thread arun
Hi, TRy this: set.seed(30)  mat1<- matrix(sample(1:50,12*12,replace=TRUE),ncol=12) mat1   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]  [1,]    5   28   13    4   47    3   30   16    3    39    42 9  [2,]   25   44   15   21    9    5   32    5    4    33    30    21  [3,

Re: [R] as.date not reading Hour/Minute am/pm Data

2013-04-15 Thread arun
Hi, Try: x<- c("2/26/13 11:59 PM", "2/25/13 10:25 AM") strptime(x,"%m/%d/%y %I:%M %p") #[1] "2013-02-26 23:59:00" "2013-02-25 10:25:00" #or library(lubridate) parse_date_time(x,"%m/%d/%y %I:%M %p") # 2 parsed with %m/%d/%y %I:%M %p #[1] "2013-02-26 23:59:00 UTC" "2013-02-25 10:25:00 UTC" A.K.

Re: [R] Remove Rows Based on Factor

2013-04-15 Thread arun
Hi, May be this helps: ZZ<- data.frame(Index=c("2006-04-07","2006-04-08","2007-01-01","2007-01-01","2008-01-04","2008-01-04"),Open=c(17.5,17.6,16.8,17.2,17.3,17.5),High=c(18.2,17.6,17.2,17.2,17.5,17.6),Low=c(17.3,16.8,16.8,16.8,16.2,15.8),Close=c(17.5,16.8,17.1,16.8,15.9,16.2),Volume=c(23834500,29

Re: [R] Remove Rows Based on Factor

2013-04-15 Thread arun
Adjusted #2006-04-07 02:30:00 17.5 18.2 17.3  17.5 23834500 16.8 #2007-01-01 02:30:00 17.2 17.2 16.8  16.8   991400 16.2 #2008-01-04 03:00:00 17.3 17.5 16.2  15.9   443522 16.5 A.K. - Original Message - From: arun To: "Sparks, John James" Cc: R help Sent: Monday,

Re: [R] group data

2013-04-15 Thread arun
Hi, You could do this: dat1<- read.table(text=" ID  Value AL1  1 AL2  2 CA1  3 CA4  4 ",sep="",header=TRUE,stringsAsFactors=FALSE)  lst1<-split(dat1,gsub("\\d+","",dat1$ID)) res<-do.call(rbind,lapply(seq_along(lst1),function(i) {x1<-lst1[[i]]; x1$group<- paste0("Key",i);x1}))  res #   ID Value gro

Re: [R] Removing multiple elements from a vector or list

2013-04-15 Thread arun
Hi,  vec1<- letters vec1[!grepl("b|r|x",alp)] # [1] "a" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "s" "t" "u" #[20] "v" "w" "y" "z"  vec1[!vec1%in% c("b","r","x") ] # [1] "a" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "s" "t" "u" #[20] "v" "w" "y" "z" alp<-lapp

Re: [R] converting blank cells to NAs

2013-04-15 Thread arun
You can use na.strings="" in read.table() or read.csv() library(stringr) vec1<-unlist(str_split(readLines(textConnection("3,7,11,,12,14,15,,17,18,19")),","))  vec1[vec1==""]<- NA  vec1 # [1] "3"  "7"  "11" NA   "12" "14" "15" NA   "17" "18" "19" #or scan(text="3,7,11,,12,14,15,,17,18,19",sep=",

Re: [R] how to add a row vector in a dataframe

2013-04-15 Thread arun
Hi, May be this helps you. #Using set.seed(12345) S=10 simdata <- replicate(S, generate(250)) lstpshat<-lapply(seq_len(ncol(simdata)),function(i) {glm.t<-glm(t~x1+x2+x3+x4+x5+x6+x7+I(x2^2)+I(x4^2)+I(x7^2)+x1:x3+x2:x4+x3:x5+x4:x6+x5:x7+x1:x6+x2:x3+x3:x4+x4:x5+x5:x6,family=binomial,data=simdata[

Re: [R] matching multiple fields from a matrix

2013-04-15 Thread arun
HI, May be this helps: dat1<- read.table(text=" site1 depth1 year1 site2 depth2 year2 10 30 1860 NA NA NA NA NA NA 50 30 1860 10 20 1850  11 20 1850 11 25 1950  12 25 1960 10 NA 1870  12 30 1960 11 25 1880  15 22  1890 14 22 1890  14 25 1880 ",sep="",header=TRUE,stringsAsFactors=FALSE)   res<-merg

Re: [R] Splitting the Elements of character vector

2013-04-16 Thread arun
Hi, Try: df = data.frame(currency_type = c("EURO_o_n", "EURO_o_n", "EURO_1w", "EURO_1w", "USD_o_n", "USD_o_n", "USD_1w", "USD_1w"), rates = c(0.47, 0.475, 0.461, 0.464, 1.21, 1.19, 1.41, 1.43),stringsAsFactors=FALSE)  df$currency<-unlist(lapply(str_split(df[,1],"_"),`[`,1))  df$tenor<-unlist(lapp

Re: [R] Splitting the Elements of character vector

2013-04-16 Thread arun
EURO   o_n 0.475 #3 EURO    1w 0.461 #4 EURO    1w 0.464 #5  USD   o_n 1.210 #6  USD   o_n 1.190 #7  USD    1w 1.410 #8  USD    1w 1.430 A.K. - Original Message - From: arun To: Katherine Gobin Cc: R help Sent: Tuesday, April 16, 2013 9:00 AM Subject: Re: [R]

Re: [R] converting blank cells to NAs

2013-04-16 Thread arun
Hi, I am not sure about the problem. If your non-numeric vector is like: a,b,,d,e,,f vec1<-unlist(str_split(readLines(textConnection("a,b,,d,e,,f")),","))  vec1[vec1==""]<- NA  vec1 #[1] "a" "b" NA  "d" "e" NA  "f" If this doesn't work, please provide an example vector. A.K. >Thanks for the re

Re: [R] how to change the date into an interval of date?

2013-04-16 Thread arun
  8 obs. of  4 variables: # $ patient_id  : num  2 2 2 2 3 3 3 3 # $ responsed_at: Date, format: "2010-05-26" "2010-07-07" ... # $ number  : num  1 2 3 4 1 2 3 4 # $ scores  : num  1 1 2 3 1 5 4 5 A.K. From: GUANGUAN LUO To: arun Sent: Tuesday

Re: [R] I don't understand the 'order' function

2013-04-16 Thread arun
Hi, vec1<- c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045) vec1[order(vec1)]  #[1]  210  210  505  920 1045 1210 1335 1545 2085 2255 2465 order(vec1)  #[1]  8  9 10  7 11  6  5  4  3  2  1 sort(vec1,index.return=TRUE) #$x  #[1]  210  210  505  920 1045 1210 1335 1545 2085 2255 2465

Re: [R] avoid losing data.frame attributes on cbind()

2013-04-16 Thread arun
HI, Not sure if this helps: library(plyr) res<-mutate(Xa,var1=round(Sepal.Length),var2=round(Sepal.Width)) str(res) #'data.frame':    150 obs. of  7 variables: # $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... # $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... # $ Petal.L

Re: [R] avoid losing data.frame attributes on cbind()

2013-04-16 Thread arun
# $ var2    : num  4 3 3 3 4 4 3 3 3 3 ... # - attr(*, "label")= chr "Some df label" A.K. - Original Message - From: arun To: Liviu Andronic Cc: R help Sent: Tuesday, April 16, 2013 2:40 PM Subject: Re: [R] avoid losing data.frame attributes on cbind() HI, N

Re: [R] avoid losing data.frame attributes on cbind()

2013-04-16 Thread arun
"label")= chr "Some df label" A.K. - Original Message - From: arun To: Liviu Andronic Cc: R help Sent: Tuesday, April 16, 2013 4:45 PM Subject: Re: [R] avoid losing data.frame attributes on cbind() Hi, Another method would be: Xc<- Xa  Xc$var1<-NA; Xc$var2<-

Re: [R] R question

2013-04-16 Thread arun
566 41.2566  # Removed.SST.AGC.QC16200 Removed.SST.AGC.Kurtosis.Skewness #1  NA    NA #2 46.1658    NA #3 41.2566    NA  # Removed.SST.AGC.Kurtosis

Re: [R] Merge

2013-04-16 Thread arun
#2  2  1  yes #3  3 22 #4  4 15 #5  5  3   no #6  6  6 #7  7  8  yes A.K. From: farnoosh sheikhi To: "smartpink...@yahoo.com" Sent: Wednesday, April 17, 2013 12:52 AM Subject: Merge Hi Arun, I want to merge a data set with another data frame with 2 columns and keep the sam

Re: [R] Problem with DateVisit-gives wrong year?

2013-04-17 Thread arun
HI, test.date<- rep(c("14/05/2012","01/10/2012","28/09/2012"),each=6)  as.Date(test.date,"%d/%m/%Y") # [1] "2012-05-14" "2012-05-14" "2012-05-14" "2012-05-14" "2012-05-14"  #[6] "2012-05-14" "2012-10-01" "2012-10-01" "2012-10-01" "2012-10-01" #[11] "2012-10-01" "2012-10-01" "2012-09-28" "2012-09-28

Re: [R] Transformation of a variable in a dataframe

2013-04-17 Thread arun
Hi, You may also try: dfab<-data.frame(A,B) library(plyr)  dfcd<-subset(mutate(dfab,C=A*2,D=B*3),select=-c(A,B)) #or  dfcd1<-subset(within(dfab,{D<-B*3;C<-A*2}),select=-c(A,B)) dfcd$C #[1] 2 4 6  dfcd$D #[1] 12 18 21 A.K. - Original Message - From: jpm miao To: r-help Cc: Sent: Wedn

Re: [R] compate tow different data frame

2013-04-17 Thread arun
Hi, dat1<- read.table(text=" V1    V2 A  1 B  2 C  1 D  3 ",sep="",header=TRUE,stringsAsFactors=FALSE) dat2<- read.table(text=" V3    V2 AAA   1 BBB   2 CCC   3 ",sep="",header=TRUE,stringsAsFactors=FALSE) library(plyr) join(dat1,dat2,by="V2",type="full") #  V1 V2 

<    5   6   7   8   9   10   11   12   13   14   >