Re: [R] splits with 0s in middle columns

2012-09-02 Thread arun
it(C," "))) colnames(res)<-paste("A",1:4,sep="") res } fun1(dat2$Composition_percent_part)     head(fun1(dat2$Composition_percent_part),5) #  A1 A2 A3 A4 #1 60 25  0 15 #2 60 25  0 15 #3 40 35 15 10 #4 40 35 15 10 #5 40 35 15 10 A.K. Fr

Re: [R] splits with 0s in middle columns

2012-09-02 Thread arun
\\1 \\2 \\3 \\4",B))) dat1<-data.frame(do.call(rbind,strsplit(C," "))) colnames(dat1)<-paste0("A",1:4) dat1 }  fun1(A) #  A1 A2 A3 A4 #1 10 20  0 30 #2 40  0  0 20 #3 60 10 10  5 #4 80  0  0 10 A.K. - Original Message - From: Rui Barradas To: Sapana Lohani

Re: [R] splits with 0s in middle columns

2012-09-02 Thread Rui Barradas
the whole column I wanted to split. Could you please have a look and suggest me the error? I ma totally stuck at this point of my analysis From: Rui Barradas To: Sapana Lohani Cc: r-help Sent: Sunday, September 2, 2012 10:05 AM Subject: Re: [R] splits w

Re: [R] splits with 0s in middle columns

2012-09-02 Thread Rui Barradas
Hello, You don't need a new function, what you need is to prepare your data in such a way that the function can process it. A <- c("percent (10/20/30)", "percent (40/20)", "percent (60/10/10/5)", "percent (80/10)") B <- gsub("\\(|\\)|percent| ", "", A) fun(B) Also, please use dput to post th

Re: [R] splits with 0s in middle columns

2012-08-31 Thread arun
HI, You can also use ifelse: dat1<-read.table(text=" 10/20/30 40/20 60/10/10/5 80/10 ",sep="",header=FALSE,stringsAsFactors=FALSE) dat2<-ifelse(nchar(gsub("[^/]","",dat1$V1))==1,gsub("(.*)/(.*)","\\1 0 0 \\2",dat1$V1),ifelse(nchar(gsub("[^/]","",dat1$V1))==2,gsub("(.*)/(.*)/(.*)","\\1 \\2 0 \\3",

Re: [R] splits with 0s in middle columns

2012-08-31 Thread arun
Hi, Try this: dat1<-read.table(text=" 10/20/30 40/20 60/10/10/5 80/10 ",sep="",header=FALSE,stringsAsFactors=FALSE) dat2<-gsub("(.*)/(.*)","\\1 0 0 \\2", gsub("(.*)/(.*)/(.*)","\\1 \\2 0 \\3", gsub("(.*)/(.*)/(.*)/(.*)","\\1 \\2 \\3 \\4",dat1$V1))) dat3<-data.frame(do.call(rbind,strsplit(dat2, " "

Re: [R] splits with 0s in middle columns

2012-08-31 Thread Rui Barradas
age in the code you sent. It says "Error in fun(A) : could not find function "paste0" I am a new in R so could not fix it. Can you help me fix that ? Thanks From: Rui Barradas To: Sapana Lohani Cc: r-help Sent: Friday, August 31, 2012 12:35

Re: [R] splits with 0s in middle columns

2012-08-31 Thread Rui Barradas
Hello, Try the following. A <- c( "10/20/30", "40/20", "60/10/10/5", "80/10") fun <- function(X){ xname <- deparse(substitute(X)) s <- strsplit(X, "/") n <- max(sapply(s, length)) tmp <- numeric(n) f <- function(x){ x <- as.numeric(x) m <- length(x)