[R] Unequal splits from a column
Rui Barradas
ruipbarradas at sapo.pt
Fri Aug 17 07:39:48 CEST 2012
Hello,
Try the following.
x <- c("slope (60/25/15)",
"slope (90/10)",
"slope (40/35/15/10)",
"slope (40/25/25/10)")
dat <- data.frame(X = x)
lst <- unlist(lapply(dat, function(.x) gsub("slope \\((.*)\\)$", "\\1",
.x)))
lst <- strsplit(lst, "/")
keep <- seq_len(max(unlist(lapply(lst, length))))
vec <- rep("0", length(keep))
result <- do.call(rbind, lapply(lst, function(.x) as.integer(c(.x,
vec)[keep])))
Now, in your case, just substitute 'dat' by the name of your vector.
(I think this solution is too complicated but it predicts for the case
where the input vector is in fact a list.)
Hope this helps,
Rui Barradas
Em 17-08-2012 06:05, Sapana Lohani escreveu:
> Hi I am new to R so am struggling with the commands here
>
> I have one column in a table that looks like
>
>
> slope (60/25/15)
> slope (90/10)
> slope (40/35/15/10)
> slope (40/25/25/10)
> I want to have 4 columns with just the number inside the parenthesis. when there is no number that cell can have 0. I want the output like this
>
> 60 25 15 0
> 90 10 0 0
> 40 35 15 10
> 40 25 25 10
>
> Can somebody help me??
> Thanks
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list