Hi, I was wondering if it's possible to separate elements in multiple rows that actually should appear in different columns. I have a file where in certain lines there are elements not separated, and they certainly should appear in different columns (an example of the file is attached). The point is that I do not want to manually add a space in the txt file, however, I did not manage to do it automatically in R... Thanks in advance for any insight. Jaime -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: test.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121130/1309566d/attachment-0002.txt> -------------- next part --------------
Hello,
Try the following.
x <- scan(text="
-100 -100-3456-3456-3456-100 -100
23 -3456-3456-189 34 56 78
-100 34 56 21 44 65 78
", what = "")
fun <- function(x){
f <- function(.x){
if(grepl("-[[:digit:]]+", .x)){
g <- gregexpr("-[[:digit:]]+", .x)
.y <- as.numeric(unlist(regmatches(.x, g)))
}else{
.y <- as.numeric(.x)
}
.y
}
unlist(unname(sapply(x, f)))
}
fun(x)
Hope this helps,
Rui Barradas
Em 30-11-2012 12:55, Jaime Otero Villar escreveu:> Hi,
>
> I was wondering if it's possible to separate elements in multiple rows
> that actually should appear in different columns. I have a file where
> in certain lines there are elements not separated, and they certainly
> should appear in different columns (an example of the file is
> attached). The point is that I do not want to manually add a space in
> the txt file, however, I did not manage to do it automatically in R...
>
> Thanks in advance for any insight.
>
> Jaime
>
>
>
>
>
> ______________________________________________
> 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.
[[alternative HTML version deleted]]
On 30-11-2012, at 13:55, Jaime Otero Villar wrote:> Hi, > > I was wondering if it's possible to separate elements in multiple rows that actually should appear in different columns. I have a file where in certain lines there are elements not separated, and they certainly should appear in different columns (an example of the file is attached). The point is that I do not want to manually add a space in the txt file, however, I did not manage to do it automatically in R... >Taking into account your description simulate file. Use readLines to read the file into a vector of lines. Use gsub() to replace each - with a single space. Finally use read.table to get a dataframe. # use the example consisting of 3 lines data.text <- "-100 -100-3456-3456-3456-100 -100 23 -3456-3456-189 34 56 78 -100 34 56 21 44 65 78" x.lines <- readLines(textConnection(data.text)) x.lines # replace - with single space x.1 <- gsub("-"," ",x.lines) x.1 read.table(text=x.1, header=FALSE) Berend
Hi,
Try this:
Lines<-"-100 -100-3456-3456-3456-100 -100
23 -3456-3456-189 34 56 78
-100 34 56 21 44 65 78
"
res<-unlist(strsplit(gsub("\\-","
-",Lines),"\n"))
res1<-do.call(rbind,lapply(lapply(split(res,seq_along(res)),function(x)
unlist(strsplit(x," "))),function(x) as.numeric(x[x!=""])))
?res1
##? [,1]? [,2]? [,3]? [,4]? [,5] [,6] [,7]
#1 -100? -100 -3456 -3456 -3456 -100 -100
#2?? 23 -3456 -3456? -189??? 34?? 56?? 78
#3 -100??? 34??? 56??? 21??? 44?? 65?? 78
?res2<-data.frame(res1)
A.K.
----- Original Message -----
From: Jaime Otero Villar <j.o.villar at bio.uio.no>
To: r-help at r-project.org
Cc:
Sent: Friday, November 30, 2012 7:55 AM
Subject: [R] how to separate stuck row elements?
Hi,
I was wondering if it's possible to separate elements in multiple rows that
actually should appear in different columns. I have a file where in certain
lines there are elements not separated, and they certainly should appear in
different columns (an example of the file is attached). The point is that I do
not want to manually add a space in the txt file, however, I did not manage to
do it automatically in R...
Thanks in advance for any insight.
Jaime
______________________________________________
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 could also try this:
Lines<-"-100 -100-3456-3456-3456-100 -100
23 -3456-3456-189 34 56 78
-100 34 56 21 44 65 78"
Lines1<-readLines(textConnection(Lines))
?res1<-unlist(strsplit(gsub("[-]"," -",Lines2),"
"))
?matrix(as.numeric(res1[res1!=""]),nrow=length(Lines1),byrow=TRUE)
??? [,1]? [,2]? [,3]? [,4]? [,5] [,6] [,7]
#[1,] -100? -100 -3456 -3456 -3456 -100 -100
#[2,]?? 23 -3456 -3456? -189??? 34?? 56?? 78
#[3,] -100??? 34??? 56??? 21??? 44?? 65?? 78
A.K.
----- Original Message -----
From: Jaime Otero Villar <j.o.villar at bio.uio.no>
To: r-help at r-project.org
Cc:
Sent: Friday, November 30, 2012 7:55 AM
Subject: [R] how to separate stuck row elements?
Hi,
I was wondering if it's possible to separate elements in multiple rows that
actually should appear in different columns. I have a file where in certain
lines there are elements not separated, and they certainly should appear in
different columns (an example of the file is attached). The point is that I do
not want to manually add a space in the txt file, however, I did not manage to
do it automatically in R...
Thanks in advance for any insight.
Jaime
______________________________________________
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.