Skip to content
Prev 304704 / 398506 Next

splits with 0s in middle columns

HI,
You can also try this as a function:
fun1<-function(x){
B<-gsub("percent{0,1}\\s\\((.*)\\)","\\1",x)
C<-gsub("(.*)/(.*)","\\1 0 0 \\2", gsub("(.*)/(.*)/(.*)","\\1 \\2 0 \\3", gsub("(.*)/(.*)/(.*)/(.*)","\\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 <ruipbarradas at sapo.pt>
To: Sapana Lohani <lohani.sapana at ymail.com>
Cc: r-help <r-help at r-project.org>
Sent: Sunday, September 2, 2012 1:05 PM
Subject: Re: [R] splits with 0s in middle columns

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 the data examples,

dput(A)
c("percent (10/20/30)", "percent (40/20)", "percent (60/10/10/5)",
"percent (80/10)")

Then copy&paste in your post.

Rui Barradas

Em 02-09-2012 04:22, Sapana Lohani escreveu:
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.