Hello,
You should Cc the list, there are others presenting solutions.
What's going on should be obvious, your data example had "percent" in
it, and your data file has "slope"!
How could you expect it to work?
Just in case, I'm changing the regular expression to removing everything
but bars and digits.
dat <- read.csv("test.csv")
B <- gsub("[^/[:digit:]]+", "", dat$Composition_percent_part)
Rui Barradas
Em 03-09-2012 01:38, Sapana Lohani escreveu:
Hello Rui,
I do not know whats wrong with my data, so am sending you 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 <ruipbarra...@sapo.pt>
To: Sapana Lohani <lohani.sap...@ymail.com>
Cc: r-help <r-help@r-project.org>
Sent: Sunday, September 2, 2012 10:05 AM
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:
Dear Rui,
The new code works fine for what I wanted. I have another similar column but it
looks like
A
percent (10/20/30)
percent (40/20)
percent (60/10/10/5)
percent (80/10)
I want a similar split but delete the percent in the front. The output should
look like
A1 A2 A3 A4
10 20 0 30
40 0 0 20
60 10 10 5
80 0 0 10
Could you please make the small change in the code that you gave me. It must be
a small edition but I could not figure that out. FYI the code that worked was
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)
if(m > 1){
tmp[n] <- x[m]
tmp[seq_len(m - 1)] <- x[seq_len(m - 1)]
}else tmp[1] <- x
tmp
}
res <- do.call(rbind, lapply(s, f))
colnames(res) <- paste(xname, seq_along(s), sep = "")
data.frame(res)
}
fun(A)
Thank you so very much.
______________________________________________
R-help@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.