Re: [R] Speed up or alternative to 'For' loop

2013-06-10 Thread arun
Hi, Some speed comparisons: df <- data.frame(TreeID=rep(1:6000,each=20), Age=rep(seq(1,20,1),6000)) df$Height <- exp(-0.1 + 0.2*df$Age) df1<- df df3<-df library(data.table) dt1<- data.table(df) df$HeightGrowth <- NA system.time({  #Rui's 2nd function df2 <- data.matrix(df) for(i in seq_len(nro

Re: [R] Combining CSV data

2013-06-10 Thread arun
  ff  #   DataComment2      DataComment3 #1 This is comment 2 This is comment 3 #2 This is comment 2 This is comment 3 A.K. - Original Message - From: arun To: Shreya Rawal Cc: R help Sent: Monday, June 10, 2013 6:41 PM Subject: Re: [R] Combining CSV data Hi, T

Re: [R] mapply on multiple data frames

2013-06-11 Thread arun
Hi, It would be better to provide a reproducible example. set.seed(25) all_dfs<- list(df1=data.frame(col1=sample(1:40,20,replace=TRUE),col2=sample(20:40,20,replace=TRUE),col3= sample(1:3,20,replace=TRUE)),df2=data.frame(col1=sample(30:60,20,replace=TRUE),col2=sample(35:65,20,replace=TRUE),col3=s

Re: [R] R vector

2013-06-11 Thread arun
HI, Not sure if this is what you wanted. mat1<- matrix(c(1, 1, -1, -1, 1, -1, -1, -2, 1, 1, 1, 1), byrow=TRUE, nc=4) fun1<- function(mat){     matP<- mat     matN<- mat     matP[matP<0]<- NA     matN[matN>0]<- NA     resP<-rowSums(matP,na.rm=TRUE)/ncol(matP)     resN<- rowSums(matN,na.rm=TRU

Re: [R] Combining CSV data

2013-06-11 Thread arun
  dd   ee   ff This is comment 1  # DataComment2  DataComment3 #1 This is comment 2 This is comment 3 #2 This is comment 2  Otherwise, you need to provide an example that matches the real dataset. A.K. From: Shreya Rawal T

Re: [R] ggplot2 error: "Error in as.environment(where) : 'where' is missing"

2013-06-11 Thread arun
Hi, dataf <- as.data.frame(cbind(Sample,Vals))  str(dataf) #'data.frame':    20 obs. of  2 variables: # $ Sample: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ... # $ Vals  : Factor w/ 20 levels "121","154","159",..: 20 12 13 1 2 14 18 5 17 10 ... ggplot(dataf,aes(x=Vals,colour=Sample))+geom_

Re: [R] Help needed in feature extraction from two input files

2013-06-11 Thread arun
Hi, Try this: lines1<- readLines(textConnection("gene1 or1|1234 or3|56 or4|793 gene4 or2|347 gene5 or3|23 or7|123456789")) lines2<-readLines(textConnection(">or1|1234 ATCGGATTCAGG >or2|347 GAACCTATCAATTTATATAA >or3|56 ATCGGAGATATAACCAATC >or3|23 TTAACAAGAGAATAGACAAA >or4|793

Re: [R] extract common rows from a dataframe

2013-06-11 Thread arun
Hi, Try this: dat1<- read.table(text=" DEPTH SALINITY DEPTH SALINITY 18    87    39.06    94    39.06 19  173    39.05  141  NA 20  260    39.00  188    39.07 21  312    38.97  207    39.03 22    1    39.36    1    39.35 23    10    39.36    10    39.33 24    20    39.36    20    39.33 25    30

Re: [R] Combining CSV data

2013-06-11 Thread arun
HI, You could use: result3<- data.frame(result2[,-5],read.table(text=as.character(result2$comment),sep="|",fill=TRUE,na.strings=""),stringsAsFactors=FALSE) colnames(result3)[5:7]<- paste0("DataComment",1:3) A.K. ____ From: Sh

Re: [R] Add a column to a dataframe based on multiple other column values

2013-06-11 Thread arun
HI, May be this helps: dat1<- read.table(text=" x1    y1    x2    y2    x3    y3    output 2    100    190    99    1430    79    89 2    100    192    63    1431    75    69 2    100    192    63    1444    51    57 3    0    195    99    1499    50    74.5 3    0    198    98    1500    80    89

Re: [R] Help needed in feature extraction from two input files

2013-06-11 Thread arun
<- strsplit(x,"\t")[[1]]; x1New<-x1[-1];x2<-  gsub(">(.*)\\n.*","\\1",lines2New);lines3<-lines2New[match(x1New,x2)];write.table(lines3,paste0(x1[1],".txt"),row.names=FALSE,quote=FALSE)}) I didn't had any problems in the output. It looks like bel

Re: [R] Combining CSV data

2013-06-12 Thread arun
is is comment 2 This is comment 3 #2    A.K. ____ From: Shreya Rawal To: arun Cc: R help ; jim holtman Sent: Wednesday, June 12, 2013 8:58 AM Subject: Re: [R] Combining CSV data Great, thanks Arun, but I seem to be running into this error.

Re: [R] Add a column to a dataframe based on multiple other column values

2013-06-12 Thread arun
0 #8 33  82 870 100 1490 97 93.0 #9 33   0 871  82 1494 85 55.7 A.K. - Original Message - From: arun To: Tom Oates Cc: R help Sent: Tuesday, June 11, 2013 5:23 PM Subject: Re: [R] Add a column to a dataframe based on multiple other column values HI, May be this helps:

Re: [R] Creating a new var from conditional values in other vars

2013-06-12 Thread arun
Hi, Try this: set.seed(25) dat1<- data.frame(A=sample(1:30,100,replace=TRUE),B=sample(1:35,100,replace=TRUE),C=sample(1:25,100,replace=TRUE)) dat1$pattern<-with(dat1,ifelse(A>20 & B<=2.5 & C<=20,"Normal",ifelse(A <20 & B >2.5 & C >20,"Increased",ifelse(A >=20 & B >2.5 & C <=20,"Low","Other"

Re: [R] new data

2013-06-12 Thread arun
  fun3   452   226   226   452 #res20F3    final3New  fun3   284   142   142   284 #res5F3_1  final3New1  fun3   288   144   144   288 #res10F3_1 final3New1  fun3   488   244   244   488 #res20F3_1 final3New1  fun3   310   155   155   310  h

Re: [R] Creating a new var from conditional values in other vars

2013-06-12 Thread arun
uot;,ifelse(A >=20 & B >2.5 & C <=20,"Low","Other" identical(dat1,dat1New) #[1] TRUE dat2<-transform(dat1,pattern=ifelse(A>20 & B<=2.5 & C<=20,"Normal",ifelse(A <20 & B >2.5 & C >20,"Increased",ifel

Re: [R] identify data points by certain criteria

2013-06-12 Thread arun
Hi, Not clear about the 'Time' column. dat1<- read.table(text=" Time    Var1  Var2 00:00    1  0 00:01    0  0 00:02    1  0 00:03    1  0 00:04    0  0 00:05    1  0 00:06    1  0 00:07    1  0 00:0

Re: [R] find the position of first observation for each subject

2013-06-13 Thread arun
HI, Try this: ds1<- data.frame(id,time) which(with(ds1,ave(time,id,FUN=seq))==1) #[1] 1 4 9 A.K. - Original Message - From: Gallon Li To: R-help@r-project.org Cc: Sent: Thursday, June 13, 2013 3:56 AM Subject: [R] find the position of first observation for each subject suppose I have

Re: [R] Remove levels

2013-06-13 Thread arun
Hi, Not sure if the OP is concerned about this: Using David's data  str(DATA) #'data.frame':    15 obs. of  2 variables: # $ UnitName_1: Factor w/ 3 levels "lake","pond",..: 3 3 1 3 2 2 3 1 2 3 ...  #$ Var   : int  95 97 12 47 54 86 14 92 88 8 ...  toBeRemoved1 <- which(DATA$UnitName_1=="lake"

Re: [R] find the position of first observation for each subject

2013-06-13 Thread arun
Also, if the ids are ordered and numeric: which(c(1,diff(ds[,1]))>0) #[1] 1 4 9 A.K. - Original Message - From: arun To: Gallon Li Cc: R help Sent: Thursday, June 13, 2013 9:40 AM Subject: Re: [R] find the position of first observation for each subject HI, Try this:

Re: [R] Search for Matching word in a Dataframe

2013-06-13 Thread arun
Hi, You could use: ?grep grep("ARUN",MyDat[,1]) #[1] 2 #or library(stringr)  which(!is.na(str_match(MyDat[,1],"ARUN"))) #[1] 2  vec1<-c(MyDat[,1],"ARUN003","Arun")  which(!is.na(str_match(toupper(vec1),"ARUN"))) #[1]  2 10 11 A.K.

Re: [R] puzzling behavior of within

2013-06-13 Thread arun
within(mtcars,{ x<-rep(0,nrow(mtcars));x[gear==4]<-1;x[gear==3]<-2}) #should fix this; A.K. - Original Message - From: Ista Zahn To: Bert Gunter Cc: r-help@r-project.org Sent: Thursday, June 13, 2013 12:17 PM Subject: Re: [R] puzzling behavior of within On Thu, Jun 13, 2013 at 12:11

Re: [R] Getting rid of rows matching a certain condition

2013-06-13 Thread arun
HI, Please check this link (http://r.789695.n4.nabble.com/Remove-levels-td4669441.html).  It was posted today. df1<-subset(df, Trt!="blank") df2<- df1 df1$Trt<- factor(df1$Trt) levels(df1$Trt) #[1] "Trt1" "Trt2" #or  df2$Trt<-droplevels(df2$Trt)  levels(df2$Trt) #[1] "Trt1" "Trt2" A.K. Hey

Re: [R] identify data points by certain criteria

2013-06-13 Thread arun
t;-which.max(c(1,diff(index(x)[indx])));index(x)[indx1] }) lst2[1:3] #$`2012-12-01` #[1] "2012-12-01 06:12:00 EST" # #$`2012-12-02` #[1] "2012-12-02 00:40:00 EST" # #$`2012-12-03` #[1] "2012-12-03 04:21:00 EST" A.K. From: Ye

Re: [R] identify data points by certain criteria

2013-06-13 Thread arun
   0 A.K. - Original Message - From: arun To: Ye Lin Cc: Sent: Thursday, June 13, 2013 10:04 PM Subject: Re: [R] identify data points by certain criteria """ e.g. 12/31, the time period where sum(Var1+Var2)=0 are:  from 00:00:00 to 00:55:00 (55 min) from 01:07:00

Re: [R] Need help on creating Adjacency matrix in R

2013-06-14 Thread arun
HI, May be this helps: dat1<- read.table(text=" Col1 Col2 Weight   A D 0.1   B C 0.4   C M 0.6   D P 0.8   E W 1   F D 1.2   G C 3.1   H M 4 ",sep="",header=TRUE,stringsAsFactors=FALSE)  vec1<- c(unique(dat1[,1]),unique(dat1[,2])) datNew<- expand.grid(vec1,vec1) colnames(datNew)<- colnames(dat1)

Re: [R] rename and concatenate name of columns

2013-06-14 Thread arun
Hi, rename_columns<- function(dat){     for(i in 2:(ncol(dat))){     names(dat)[i]<- paste(names(dat)[1],names(dat)[i],sep="_")         } dat } dat1<- read.table(text=" chr    pos    ref    alt chr1    5    A    G chr1    8    T    C chr2    2    C    T ",sep="",header=TRUE,stringsAsFactors=FALSE

Re: [R] Removing "NA" from matrix

2013-06-14 Thread arun
HI, Try: dat1<-dat[sapply(dat,function(x) length(unique(x)))>1] cor(dat1) #   ABC DEF JKL    MNO #ABC  1.000 -0.75600764  0.55245223 -0.2735585 #DEF -0.7560076  1. -0.06479082  0.2020781 #JKL  0.5524522 -0.06479082  1.  0.4564568 #MNO -0.2735585  0.2

Re: [R] Removing "NA" from matrix

2013-06-14 Thread arun
.06479082  0.2020781 #JKL  0.5524522 -0.06479082  1.  0.4564568 #MNO -0.2735585  0.20207810  0.45645683  1.000 A.K. - Original Message ----- From: arun To: Katherine Gobin Cc: R help Sent: Friday, June 14, 2013 10:34 AM Subject: Re: [R] Removing "NA" from matrix

Re: [R] matched samples, dataframe, panel data

2013-06-14 Thread arun
==1)) #[1] 273  nrow(subset(res5F3High,dummy==0)) #[1] 273  nrow(subset(res10F3,dummy==0)) #[1] 231  nrow(subset(res10F3,dummy==1)) #[1] 231  nrow(subset(res10F3_1,dummy==1)) #[1] 237  nrow(subset(res10F3_1,dummy==0)) #[1] 237  dim(unique(res5F3)) #[1] 278   5 dim(unique(res5F3High)) #[1] 546  

Re: [R] combination of columns in a matrix

2013-06-14 Thread arun
Hi, sapply(colnames(m)[-ncol(m)],function(i) {x1<-cbind(m[,i],m[,ncol(m)]); length(which(x1[,1]!=0 & x1[,2]!=0))}) #A B C #2 1 2 A.K. - Original Message - From: Hermann Norpois To: r-help Cc: Sent: Friday, June 14, 2013 3:51 PM Subject: [R] combination of columns in a matrix Hello

Re: [R] Plotting two y-axis vs non-numeric x-axis

2013-06-15 Thread arun
Hi, You could use ?twoord.plot() from library(plotrix): library(plotrix) dat2<- data.frame(month,music,actor) dat2$month<- factor(month,labels=c(month.abb,"Pag")) dat2New<-dat2[order(dat2$month),]  with(dat2New,twoord.plot(month,music,actor,     lylim=c(0,1),rylim=c(0,13000)

Re: [R] transform summary table list to summary table (for .csv output)

2013-06-15 Thread arun
Hi, You could try this: #In one of the list element SampleSummary$ConcLow #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's # 0.4200  0.6100  0.7300  0.7373  0.8150  1.6600   1 #additional entry for NA.  You may need to check the data and remove the missing value.  Here, I removed t

Re: [R] Calculate days with R

2013-06-15 Thread arun
Hi, May be this helps: dat1<- read.table(text=" pbnr    dat  dep  dys  sop  ago  mis age female 1 10023 1994-02-21 0.75 1.00 0.50 0.50 0.75  35  1 2 10023 1994-05-25 0.75 1.00 0.50 0.50 0.75  35  1 3 10028 1994-02-01 2.00 1.75 3.00 0.50 1.50  42  1 4 10028 1999-01-15 1.25 0.75 2.25

Re: [R] quick Help needed

2013-06-15 Thread arun
Hi, It is better to ?dput() the dummy dataset. I made up some data for female==0 dat1<- read.table(text="  pbnr    dat  dep  dys  sop  ago  mis age female messpunkt2 messpunkt1 tage eintrittsjahr 1 10023 1994-02-21 0.75 1.00 0.50 0.50 0.75  35  1   8817 8817    0  1994 2 10023

Re: [R] extract all numbers from a string

2013-06-15 Thread arun
HI, One way would be: library(stringr) tmpstr = "The first number is: 32.  Another one is: 32.1. Here's a number in scientific format, 0.3523e10, and another, 0.3523e-10, and a negative, -313.1" pattern<- "(\\d)+|(\\d+\\.\\d+)|(-\\d+\\.\\d+)|(\\d+.\\d+e\\d+)|(\\d+\\.\\d+e-\\d+)" str_extract_all

Re: [R] matched samples, dataframe, panel data

2013-06-16 Thread arun
et(res5PercentLow,dummy==1)) #[1] 155 res20PercentHigh<- fun1New(final3New1,0.20,5) dim(res20PercentHigh) #[1] 846   5  dim(unique(res20PercentHigh)) #[1] 846   5  nrow(subset(res20PercentHigh,dummy==0)) #[1] 423  nrow(subset(res20PercentHigh,dummy==1)) #[1] 423 A.K. - Ori

Re: [R] selecting observations

2013-06-17 Thread arun
Hi, May be this helps. set.seed(29) GLSS<- data.frame(hhid=sample(1:10,30,replace=TRUE),value=rnorm(30)) IDstoSelect<- c(1,5,7)  GLSS[!is.na(match(GLSS[,1],IDstoSelect)),] #   hhid   value #1 1 -0.34216061 #6 1  0.28240263 #14    7  0.04197496 #17    7  0.15005878 #21    7 -1.18640939

Re: [R] Inputs for plots

2013-06-17 Thread arun
.07374,36.64006,48.15659,61.61155,77.95515,100.152)  dat1<- data.frame(X,Y) dat2<-dat1[order(dat1$Y,dat1$X),] dat2$X<-factor(dat2$X,levels=dat2$X) plot(as.numeric(dat2$X),dat2$Y,xaxt="n",xlab="X",ylab="Y",type="b")  axis(1,at=as.numeric(dat2$X),labels=lev

Re: [R] Combining CSV data

2013-06-17 Thread arun
Comment 2, Comment 3  Comment 1  Comment 2 Comment 3 #2  Comment 1a, Comment 2a Comment 1a Comment 2a  A.K. From: Shreya Rawal To: arun Cc: R mailing list ; jim holtman Sent: Monday, June 17, 2013 9:55 AM Subject: Re: [R] Combining CSV data Hi Arun, Sorry to bother y

Re: [R] vector question

2013-06-17 Thread arun
May be this helps: d[cumsum(sapply(d,function(x) any(x>b)))>=1] #[1] 12 24 36 A.K. - Original Message - From: Andras Farkas To: r-help@r-project.org Cc: Sent: Monday, June 17, 2013 2:29 PM Subject: [R] vector question Dear All, would you please help with the following: let us say I h

Re: [R] vector question

2013-06-17 Thread arun
HI Andras, Sorry, I misunderstood your question: Try this:  sapply(sapply(b,function(x) d[x To: arun Cc: Sent: Monday, June 17, 2013 2:48 PM Subject: Re: [R] vector question Arun, thank you. Looking at it I am wondering if there is a way to get 36 in the result instead of the 24. The 2nd

Re: [R] help with text patterns in strings

2013-06-17 Thread arun
Hi, May be this helps: dat1<- data.frame(Ans=c("Friday","Wednesday","Friday,Saturday,Sunday","Saturday","Sat,Sun","Th,F,Sa"),stringsAsFactors=FALSE)  dat1 Ans 1 Friday 2  Wednesday 3 Friday,Saturday,Sunday 4   Saturday 5   

Re: [R] help with text patterns in strings

2013-06-17 Thread arun
  F Sa # 2  0  0  1  1  3  4 #Or Using Bills' solution: dayNames[pmatch(vec2New,dayNames,duplicates.ok=TRUE)] # [1] "Friday"    "Wednesday" "Friday"    "Saturday"  "Sunday"    "Saturday"  #[7] "Saturday"  "Sunday" 

Re: [R] write a function to do pairwise calculation

2013-06-17 Thread arun
Hi, You didn't provide any information about the package.  I guess it is from "energy".  library(energy) x <- iris[1:50, 1:4]   #examples given in the package y <- iris[51:100, 1:4] z<- iris[101:150,1:4] vec1<-c("x","y","z") mat1<- combn(vec1,2) sapply(split(mat1,col(mat1)),function(.dat) dc

Re: [R] help with string split in R

2013-06-17 Thread arun
gsub("_.*","",myInput) #[1] "afg3"  "alg12" "dbp3"  "elp4"  "fob1"  "gpr1" A.K. - Original Message - From: CompBiol ATL To: r-help@r-project.org Cc: Hong Qin Sent: Monday, June 17, 2013 6:52 PM Subject: [R] help with string split in R #I want to remove '_alpha' in a vector of strings

Re: [R] find closest value in a vector based on another vector values

2013-06-18 Thread arun
Hi, Perhaps this works: a[sapply(b,function(x) which.min(abs(x-a)))] #[1]  8 32 A.K. - Original Message - From: Andras Farkas To: r-help@r-project.org Cc: Sent: Tuesday, June 18, 2013 8:34 AM Subject: [R] find closest value in a vector based on another vector values Dear All, would y

Re: [R] Problem with the mod function %%

2013-06-18 Thread arun
Hi, Possibly, R FAQ: 7.31 http://www.hep.by/gnu/r-patched/r-faq/R-FAQ_82.html (1.1%%1)-0.1 #[1] 8.326673e-17 fun1 <- function(x, y, eps = .Machine$double.eps^0.5) abs(x - y) < eps  fun1(1.1%%1,0.1) #[1] TRUE A.K. 0.1%%1==0.1 returns TRUE, which is right But 1.1%%1==0.1 returns FALSE !!

Re: [R] Elementary Help

2013-06-18 Thread arun
Hi, May be this helps: set.seed(24) dat1<- data.frame(ID=1:200,value=sample(c(5:200,NA),200,replace=TRUE))  which(is.na(dat1$value)) #[1]  56 146 184 sum(which(is.na(dat1$value)))  #Not clear about the 2nd part of the question #[1] 386  sum(is.na(dat1$value)) #[1] 3 table(is.na(dat1$value)) #FALSE

Re: [R] Problem with the mod function %%

2013-06-18 Thread arun
Hi Benjamin, Thanks for pointing that out. Sorry, didn't read the question carefully. (0.1%%1)-0.1 #[1] 0  (1.1%%1)-0.1 #[1] 8.326673e-17 (1%%1.1) #[1] 1 A.K. From: Benjamin To: arun Sent: Tuesday, June 18, 2013 11:33 AM Subject: Re: [R] Problem

Re: [R] transform 3 numeric vectors empty of 0/1

2013-06-18 Thread arun
Hi, You could also use: dat1<- data.frame(Vec1,Vec2,Vec3) Reduce(paste0,lapply(seq_len(ncol(dat1)),function(i) {x1<- as.character(factor(dat1[,i],labels=c("",LETTERS[i])))})) # [1] "ABC" "ABC" ""    "AC"  "AB"  "ABC" "A"   "B"   "ABC" "AC"  "B"   "A"  #[13] "AB"  "C"   "B"  #or library(plyr) lib

Re: [R] data selection

2013-06-18 Thread arun
Hi, May be this helps: set.seed(24) B101<- data.frame(SID=sample(400:450,20,replace=FALSE),zGRADE=sample(0:10,20,replace=TRUE),col3=rnorm(20)) set.seed(28) B2A<- data.frame(SID=sample(400:600,50,replace=FALSE),zGRADE=sample(0:10,50,replace=TRUE),col3=rnorm(50)) set.seed(35) B2B<- data.frame(SID=

Re: [R] write a function to do pairwise calculation

2013-06-19 Thread arun
rror in cor(A, D) : incompatible dimensions A.K. ____ From: Amanda Li To: arun Sent: Wednesday, June 19, 2013 1:11 AM Subject: Re: [R] write a function to do pairwise calculation Hello, Thanks for your help! I am a novice in R, and may I ask how I am suppose

Re: [R] quantile

2013-06-19 Thread arun
Hi, May be this helps: set.seed(28)  x<- sample(1:40,20,replace=TRUE)  qx<-quantile(x,probs=0.10)  qx #10% #3.8  qx+1 #10% #4.8 attr(qx,"names")<-NULL qx #[1] 3.8  qx+1 #[1] 4.8 A.K.   - Original Message - From: Francesco Miranda To: "r-help@r-project.org" Cc: Sent: Wednesday, J

Re: [R] Elementary Help

2013-06-19 Thread arun
s,dat1$ID)) #[1] 2 A.K. Hi, Unfortunately somehow it won't help. The unused values are not NA, the unused values are simply not there. Since these are student Ids, for instance there is no 4,8,9 etc... I need to find out which of these are not there. - Original Message - From:

Re: [R] How to combine and transpose lists

2013-06-19 Thread arun
Hi, Please use ?dput().  If `lst1` is the list. do.call(rbind,lapply(lst1,t)) #  Estimate  Std. Error   t value   Pr(>|t|) #card1  0.000577912 0.003956905 0.1460515 0.88388493 #card2  0.005164347 0.003311546 1.5594972 0.11892405 #card3  0.002682773 0.003683422 0.7283372 0.46643165 #card

Re: [R] How to combine and transpose lists

2013-06-19 Thread arun
856 0.25225436 #resp1  0.005552192 0.007135446 0.7781142 0.43652806 #resp2  0.010337814 0.004848899 2.1319923 0.03304227 #resp3  0.002850401 0.006422570 0.4438100 0.65719368 #respir 0.006212050 0.004121313 1.5072989 0.13177931 A.K. - Original Message - From: arun To: R help Cc: Sent: Wednes

Re: [R] to build a data.frame

2013-06-19 Thread arun
Hi, May be this helps: library(plyr)  ddply(df,.(Country,Iso),summarize,NbrProj=length(Project)) #    Country Iso NbrProj #1  Burkina Faso  BF   2 #2  Ethiopia  ET   1 #3 Ghana  GH   1 #4 Kenya  KE   2 #5    Madagascar  MG   2 #6  Mali  ML   1

Re: [R] Elementary Help

2013-06-19 Thread arun
LouisTalbot 137357 115 NoahStorey 137357 92 GraceChapman etc... The exact quesiton is the following: Some ids in the range 1 to 161 are unused (e.g. 4,7,9). In fact, there are 30 unused pupil Ids  between 1 and 161. What is the sum of these 30 integers? - Original Message -

Re: [R] alternative to ifelse

2013-06-19 Thread arun
Hi, May bet this also helps: set.seed(12345) x <- c(1:30) x1 <- sample(x, 15, replace = TRUE) x2 <- sample(x, 15, replace = TRUE) x3 <- sample(x, 15, replace = TRUE) x4 <- sample(x, 15, replace = TRUE) indx<-1+2*(x1==1)+4*(x2==1)+8*(x3==1)+16*(x4==1)  as.numeric(indx!=1) #[1] 0 1 0 0 0 0 0 0 0 0 0

Re: [R] help with text patterns in strings

2013-06-19 Thread arun
,"mid","late","wknd"))  with(dat1,tapply(res1,list(Name),FUN=sum)) #early   mid  late  wknd  #   0 1 4 6 #or  sapply(split(res1,names(vec3)),sum) #early  late   mid  wknd  #   0 4 1 6 A.K. - Original Message - From: "Crombie

Re: [R] Returning name of dataframe?

2013-06-19 Thread arun
Hi, May be this helps: xyz<-function(x,y){   z<-x+y   print(paste("the sum of", deparse(substitute(x)), "and", deparse(substitute(y)),"is", sep=" "))   z } xyz(a,b) [1] "the sum of a and b is" [,1]    [,2]    [,3]    [,4]    [,5]  [1,] -1.38805146 -1.15706888 -1.0942

Re: [R] Reading a Vector into R

2013-06-20 Thread arun
HI, You could save it in a .txt file. (e.g. "vec1.txt")  source("vec1.txt")  length(drg_470) [1] 4495  head(drg_470) #[1]  7558  9485 10481 10609 10650 10891 str(drg_470) # num [1:4495] 7558 9485 10481 10609 10650 ... A.K. Hi there, I am having trouble successfully reading this vector into R:

Re: [R] a question to transform a dataframe empty 0/1

2013-06-20 Thread arun
Hi, Not sure if you wanted the entries with "0". library(reshape2)  dfMelt<-melt(df,id.var=c("Country","Iso")) #subset those with "1" dfNew<- subset(dfMelt,value==1,select=-4) row.names(dfNew)<- 1:nrow(dfNew)  dfNew #    Country Iso   variable #1  Zimbabwe  ZW  Abaco #2  South Afri

Re: [R] adding by categories in a data frame???

2013-06-20 Thread arun
Hi, dat1<- read.table(text=" name   number a    2 a    3 b    5 b    7 c 9 c 1 ",sep="",header=TRUE,stringsAsFactors=FALSE) aggregate(number~name,data=dat1,sum) #  name number #1    a  5 #2    b 12 #3    c 10 #or library(plyr)

Re: [R] Choosing subset of data.frame

2013-06-20 Thread arun
HI, You could also use: beta_results[!is.na(match(beta_results[,1],instru)),] #  instrument beta_values #3    JKL   0.529 #6    STU  -1.080 #8    XYZ   0.420 #If there are no duplicates, this could also work beta_results[match(instru,beta_results[,1]),] #  instrument bet

Re: [R] keep row names into data

2013-06-21 Thread arun
Hi, Try: dat1<- read.csv("mydata.csv",sep="\t",row.names=1)  #sep could be different in your case.  dat1 #   A B C D E #Jhon   1 3 6 9 8 #Kelly  2 3 4 6 9 #Audrey 3 5 6 9 7 A.K. Hi, I am calling my csv data with 37 columns and 16 rows into R. However I really need to keep coloumns name

Re: [R] How to define desired numbers to a vector based on the present numbers

2013-06-21 Thread arun
HI, May be this also works: plot(x,col=as.numeric(factor(x))) A.K. - Original Message - From: Rui Barradas To: Suparna Mitra Cc: r-help@r-project.org Sent: Friday, June 21, 2013 11:32 AM Subject: Re: [R] How to define desired numbers to a vector based on the present numbers Hello,

Re: [R] extracting data coincident with the beginning and end of multiple streaks (rle)

2013-06-21 Thread arun
Hi, May be this helps: ###Added more lines of fake data fn_hp<- read.table(text=" id   date  wl_m    wet    cuml_day 585 fn 2012-03-03   0.1527048   1    1  586 fn 2012-03-04   0.2121408   1    2  587 fn 2012-03-05   0.1877568   1    3   

Re: [R] Apply acf to data frame containing 'NA'

2013-06-23 Thread arun
Hi, May be this helps: set.seed(24) A <- matrix(sample(c(NA,rnorm(1500)),1500,replace=FALSE),nrow=500) par(mfrow=c(ncol(A),1))  lapply(split(A,col(A)),function(x) acf(na.omit(x))) A.K. - Original Message - From: Zilefac Elvis To: "r-help@r-project.org" Cc: Sent: Sunday, June 23, 2013

Re: [R] Apply acf to data frame containing 'NA'

2013-06-23 Thread arun
HI, dat1<- read.csv("AMS3.csv",sep="\t",row.names=1) pdf("AMS3_acf.pdf")  lapply(dat1,function(x)acf(na.omit(x))) dev.off() A.K. ____ From: Zilefac Elvis To: arun Cc: R help Sent: Sunday, June 23, 2013 1:51 PM Subject:

Re: [R] Wilcox paired test error message

2013-06-23 Thread arun
HI, May be this helps: dat1<- read.table(text=" beforesmall aftersmall 63.5  512.0 54.5  237.5 52.5  161.5 78.0  153.5 53.5   68.0 50.5   65.5 69.0   52.0 76.0   59.0 68.0   66.5 75.5   66.5 67.0   45.5 81.0   54.5 49.0   44.0 51.0   42.5 53.0

Re: [R] Apply acf to data frame containing 'NA'

2013-06-24 Thread arun
Hi, Try this: res1<-do.call(cbind,t(sapply(res,c))[,1]) #get the acf values only.  Assuming that is what you wanted.  write.csv(res1,"AMSacf.csv",row.names=FALSE) A.K. From: Zilefac Elvis To: arun Sent: Monday, June 24, 2013 1:00 PM Su

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread arun
 f1<- function(x){env<- parent.frame();env$a<-5; g(x)} f1(2) #[1] 7  f1(7) #[1] 12  f1(5) #[1] 10 A.K. From: David Kulp To: "r-help@r-project.org" Sent: Monday, June 24, 2013 4:27 PM Subject: [R] Lexical scoping is not what I expect According to http://cr

Re: [R] Fetch and merge from a data set

2013-06-25 Thread arun
HI, Try this: merge(data,data1["NAME"]) A.K. - Original Message - From: Nico Met To: Rui Barradas Cc: R help Sent: Tuesday, June 25, 2013 6:00 AM Subject: Re: [R] Fetch and merge from a merge(dat1["NAME"],dat2) data set Many thanks Rui, However If I want to extract only first colum

Re: [R] Calculate geometric mean with tapply

2013-06-25 Thread arun
May be this also works:  exp(mean(log(abs(x #[1] 0.4985282 A.K. - Original Message - From: PIKAL Petr To: Shane Carey Cc: "r-help@r-project.org" Sent: Tuesday, June 25, 2013 8:50 AM Subject: Re: [R] Calculate geometric mean with tapply Hm > -Original Message- > From: r

Re: [R] Transpose of the rows

2013-06-26 Thread arun
 dta[,3]<-as.numeric(as.character(gsub(",",".",dta[,3]))) library(reshape2)   dcast(dta,id~name,value.var="value") #    id   AreaCycl   AreaEmer   AreaMax  HeightMax nrfilledseed #1 TN00016-003A -1.3998944 -3.2771509 -3281.077   8.434493    1.825 #2 TN00016-014A -0.5843794 -0.8428928 -

Re: [R] Error when using median as aggregation function in dcast

2013-06-26 Thread arun
Hi, Please check this link: http://stackoverflow.com/questions/4835202/error-with-custom-aggregate-function-for-a-cast-call-in-r-reshape2 A.K. - Original Message - From: Johannes Radinger To: R help Cc: Sent: Wednesday, June 26, 2013 4:36 AM Subject: [R] Error when using median as ag

Re: [R] XYZ data

2013-06-26 Thread arun
Hi, Not sure whether there is any pattern in adding the small number. dat1<- read.table(text=" X  Y Z 15   20  30 15   20  40 ",sep="",header=TRUE) dat1[,1:2]<

Re: [R] match rows of R

2013-06-26 Thread arun
Hi, Try:  roweqv<- function(m,v) which(!is.na(match(interaction(as.data.frame(m),drop=TRUE),paste(v,collapse="." v<- c(2,5,8) roweqv(m,v) #[1] 2 set.seed(24) m1<-matrix(sample(1:15,3e5,replace=TRUE),ncol=3) v1<- c(10,12,4)  system.time(res<- roweqv(m1,v1))   # user  system elapsed   #0.132

Re: [R] match rows of R

2013-06-26 Thread arun
  0.312   1.076 identical(res,res2) #[1] TRUE A.K. - Original Message - From: arun To: Sachinthaka Abeywardana Cc: R help Sent: Wednesday, June 26, 2013 3:26 PM Subject: Re: [R] match rows of R Hi, Try:  roweqv<- function(m,v) which(!is.na(match(interaction(as.data.fr

Re: [R] Combining levels

2013-06-27 Thread arun
e"  Data1[,1] #[1] Residential-future   Open space-managed   Mixed use   #[4] Residential-existing Residential-existing Residential-future  #[7] Residential-existing Mixed use   #4 Levels: Mixed use Open space-managed ... Residential-future A.K.

Re: [R] multiple csv files for T-test

2013-06-27 Thread arun
Hi, May be this helps: #You can use ?read.csv() to read the two files. set.seed(24) dat1<- as.data.frame(matrix(sample(20:40,40,replace=TRUE),ncol=4)) set.seed(285) dat2<- as.data.frame(matrix(sample(35:60,40,replace=TRUE),ncol=4)) sapply(colnames(dat1),function(i) t.test(dat1[,i],dat2[,i],paired=

Re: [R] multiple csv files for T-test

2013-06-27 Thread arun
sv("file1.csv") > data2 <-read.csv("file2.csv") > dat1<- as.data.frame(matrix(data1)) > dat2<- as.data.frame(matrix(data2)) > sapply(colnames(dat1),function(i) > t.test(dat1[,i],dat2[,i],paired=TRUE)$p.value) As far as I can see all my values are

Re: [R] when to use & pros/cons of dataframe vs. matrix?

2013-06-27 Thread arun
Hi, set.seed(24) dat1<-data.frame(X=sample(letters,20,replace=TRUE),Y=sample(1:40,20,replace=TRUE),stringsAsFactors=FALSE) mat1<-as.matrix(dat1)  sapply(dat1,class) #  X   Y #"character"   "integer" sapply(split(mat1,col(mat1)),class) #  1   2 #"character" "chara

Re: [R] LSD comparison

2013-06-27 Thread arun
Hi,Without any reproducible example or code, it is difficult to understand the problem.  Did you load the package? Using the example in ?LSD.test() library(agricolae) data(sweetpotato)   model<-aov(yield~virus, data=sweetpotato)   out <- LSD.test(model,"virus", p.adj="bonferroni",   ma

Re: [R] using "rollapply" to calculate a moving sum or running sum?

2013-06-27 Thread arun
Hi, Try: library(zoo) rollapply(t(mymatrix),width=12,FUN=sum,by.column=T,fill=NA,partial=FALSE,align="left")  #     [,1] [,2] [,3] [,4] [,5]  #[1,]  342  354  366  378  390  #[2,]  402  414  426  438  450  #[3,]  462  474  486  498  510  #[4,]  522  534  546  558  570  #[5,]  582  594  606  618  6

Re: [R] multivariate version of aggregate

2013-06-27 Thread arun
Hi, May be this also helps: library(data.table) dt1<- data.table(dat) dt1[,cor(x,y),by=g] #   g  V1 #1: A -0.05643063 #2: B  0.16465040 dt1[,cor(x,y),by=g]$V1 #[1] -0.05643063  0.16465040 A.K. - Original Message - From: David Carlson To: 'Jannis' ; 'r-help' Cc: Sent: Thursday,

Re: [R] matrix data generation under 2 loops

2013-06-27 Thread arun
Hi, May be this helps: res1<-as.matrix(transform(expand.grid(k1=0.3*(1:10),k2=1:10),k3=k1*k2)) A.K. Dear all, I'm trying to generate a matrix dataset following 2 loops analysis, such as the follows: for (i in 1:10)  { k1 <- 0.3 x i for (j in 1:10)  { k2 <- j k3 <- k1*k2 }}

Re: [R] multivariate version of aggregate

2013-06-28 Thread arun
Hi,  set.seed(45)  testframe <- data.frame(a=rnorm(100), b = rnorm(100))  indices  <- rep(c(1,2), each = 50) library(plyr) ddply(testframe,.(indices),summarize, Cor1=cor(a,b)) #  indices Cor1 #1   1  0.002770524 #2   2 -0.10173 A.K. - Original Message - From: J

Re: [R] multiple csv files for T-test

2013-06-28 Thread arun
-04 A.K. thanks very much, you're help is much appreciated. Just another small question, what's the best way to deal with missing data?  If i want to do a paired t-test? - Original Message - From: arun To: R help Cc: Sent: Thursday, June 27, 2013 1:47 PM Subject: Re: mul

Re: [R] Help with tables

2013-06-28 Thread arun
HI, May be this helps: dat1<- read.table(text=" date1 time  date    timeSec topic pupilId correct 02/01/2013 14:58 02/01/2013 140323 fdp.fdp 40 TRUE 02/01/2013 14:59 02/01/2013 140372 fdp.fdp 150 TRUE 03/01/2013 11:23 03/01/2013 213833 fdp.percentage_calc_foundation 15 TRUE 03/0

Re: [R] To transform a dataframe

2013-06-29 Thread arun
HI, Not sure why df2 didn't include Cat3, Debut3, Fin3, instead included columns with all missing values (Cat5, Debut5, Fin5). library(plyr) library(reshape2) df1New<-ddply(df1,.(Mat),transform,castCat=paste0("Cat",Cat),castDébut=paste0("Début",Cat),castFin=paste0("Fin",Cat)) res<- join_all(list

Re: [R] Help: argument is not numeric or logical: returning NA

2013-06-30 Thread arun
Hi, One problem with your example dataset is that you have 11 list elements with the last one having different dimensions.  I think it was based on `summarized`. Then, the second problem I encountered for testing is that the dataset you provided doesn't fulfill the criteria. lst1<- list(structur

Re: [R] Replacing strings to numbers

2013-07-02 Thread arun
Hi, Not sure how your data looks like.  May be this helps. dat1<- read.table(text=" col1 AA-50 AT-34 TT-57 TT-45 TA-42 ",sep="",header=TRUE,stringsAsFactors=FALSE) vec1<-gsub("\\-.*","",dat1[,1]) vec2<- ifelse(vec1=="AA",-1,ifelse(vec1=="AT",0, ifelse(vec1=="TT",1,NA))) library(stringr)  abs(vec2

Re: [R] Recoding variables based on reference values in data frame

2013-07-03 Thread arun
Hi, May be this helps: Kgeno<- read.table(text=" SNP_ID SNP1 SNP2 SNP3 SNP4 Maj_Allele C G  C  A Min_Allele T A T G  ID1 CC    GG    CT    AA ID2 CC    GG    CC AA ID3 CC    GG nc  AA ID4  _  _  _  _ ID5 CC    GG    CC    AA ID6 CC    GG    CC  AA ID7 CC    GG    CT    AA ID8 _ _ _ _  ID9 CT   

Re: [R] matching similar character strings

2013-07-03 Thread arun
5 A.K. - Original Message - From: A M Lavezzi To: r-help Cc: Sent: Tuesday, July 2, 2013 10:22 AM Subject: Re: [R] matching similar character strings Dear Arun, please excuse me for this late reply, we had to stop working on this temporaririly. Let me reproduce here tw

Re: [R] nth root of matrix

2013-07-03 Thread arun
 sign(a)*abs(a)^(1/3) #  [,1] #[1,] -1.00 #[2,] -1.259921 #[3,] -1.442250 A.K. - Original Message - From: Sachinthaka Abeywardana To: "r-help@r-project.org" Cc: Sent: Tuesday, July 2, 2013 11:11 PM Subject: [R] nth root of matrix Hi all, I want to do the following: a=

Re: [R] Combine two variables

2012-09-11 Thread arun
Hi,   I am not sure how you describe combine. Try this: df1<-subset(df, !is.na(var1) &!is.na(var2)) df1$new<-paste0(df1$var1,df1$var2) > head(df1) #  var1 var2 new #1    b    a  ba #2    c    b  cb #3    b    b  bb #5    a    a  aa #6    b    b  bb #7    a    b  ab A.K. - Original Message

Re: [R] Sort or Permutate

2012-09-11 Thread arun
Hi, You can also use: within(small,{order(value)}) #   file.name value #1 /storage/storage0/59Off.Rtable 0.2203863 #3 /storage/storage0/5912314ff.Rtable 0.2886594 #2    /storage/storage0/5912On.Rtable 0.4052370 A.K. - Original Message - From: Berend Has

<    9   10   11   12   13   14   15   16   17   18   >