An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120831/6505e12a/attachment.pl>
splits with 0s in middle columns
4 messages · Sapana Lohani, Rui Barradas, arun
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)
tmp[n] <- x[m]
tmp[seq_len(m - 1)] <- x[seq_len(m - 1)]
tmp
}
res <- do.call(rbind, lapply(s, f))
colnames(res) <- paste0(xname, 1:ncol(res))
data.frame(res)
}
fun(A)
Hope this helps,
Rui Barradas
Em 31-08-2012 17:10, Sapana Lohani escreveu:
Hi, A column of my df looks like A 10/20/30 40/20 60/10/10/5 80/10 I want to split it such that the last column has the last composition and if there are not enough the middle columns get the 0s. That way my df would look like A1 A2 A3 A4 10 20 0 30 40 0 0 20 60 10 10 5 80 0 0 10 How can I do that ?? [[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.
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, " ")))
?colnames(dat3)<-paste0("A",1:4)
?dat3
#? 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: Sapana Lohani <lohani.sapana at ymail.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Friday, August 31, 2012 12:10 PM
Subject: [R] splits with 0s in middle columns
Hi,
A column of my df looks like
A
10/20/30
40/20
60/10/10/5
80/10
I want to split it such that the last column has the last composition and if there are not enough the middle columns get the 0s. That way my df would look like
A1 A2 A3 A4
10 20 0 30
40 0 0 20
60 10 10 5
80 0 0 10
How can I do that ??
??? [[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.
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",dat1$V1),gsub("(.*)/(.*)/(.*)/(.*)", "\\1 \\2 \\3 \\4",dat1$V1)))
?dat3<-data.frame(do.call(rbind,strsplit(dat2, " ")))
?colnames(dat3)<-paste0("A",1:4)
?dat3
#? 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: Sapana Lohani <lohani.sapana at ymail.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Friday, August 31, 2012 12:10 PM
Subject: [R] splits with 0s in middle columns
Hi,
A column of my df looks like
A
10/20/30
40/20
60/10/10/5
80/10
I want to split it such that the last column has the last composition and if there are not enough the middle columns get the 0s. That way my df would look like
A1 A2 A3 A4
10 20 0 30
40 0 0 20
60 10 10 5
80 0 0 10
How can I do that ??
??? [[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.