Hi every one,
I have a text file like this:
1 4 4 1 6 23
1 4 2 2 3 28
1 4 5 1 2 24
1 2 3 1 1 24
1 2 3 1 2 40
1 2 3 1 4 22
I want to separate columns by comma, like this:
1,4,4,1,6,23
1,4,2,2,3,28
1,4,5,1,2,24
1,2,3,1,1,24
1,2,3,1,2,40
1,2,3,1,4,22
I used this code:
mydata=read.table("data.txt",sep=",")
but I see this output:
1\t4\t4\t1\t6\t231\t4\t2\t2\t3\t28
...
please correct my code
Thanks in advance
Elham
[[alternative HTML version deleted]]
Hi Elham,>From the (\t), It looks like the data in your data.txt file istab-delimited. To take care of that, set the separator string to be: "sep '\t' " instead of "," when you read in the file. text <- read.table(file = "data.txt", sep = "\t") text write.table(text, "textfile.csv", row.names = F, col.names = F, sep =",") Richard On Fri, Dec 13, 2013 at 7:34 AM, Patty Haaem <elham_h763@yahoo.com> wrote:> Hi every one, > I have a text file like this: > 1 4 4 1 6 23 > 1 4 2 2 3 28 > 1 4 5 1 2 24 > 1 2 3 1 1 24 > 1 2 3 1 2 40 > 1 2 3 1 4 22 > > I want to separate columns by comma, like this: > 1,4,4,1,6,23 > 1,4,2,2,3,28 > 1,4,5,1,2,24 > 1,2,3,1,1,24 > 1,2,3,1,2,40 > 1,2,3,1,4,22 > > I used this code: > mydata=read.table("data.txt",sep=",") > but I see this output: > 1\t4\t4\t1\t6\t231\t4\t2\t2\t3\t28 > ... > please correct my code > Thanks in advance > Elham > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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]]
Hi,
Try:
mydata <- read.table("data.txt") #or
read.table("data.txt",sep="")
?str(mydata)
#'data.frame':??? 6 obs. of? 6 variables:
# $ V1: int? 1 1 1 1 1 1
# $ V2: int? 4 4 4 2 2 2
# $ V3: int? 4 2 5 3 3 3
# $ V4: int? 1 2 1 1 1 1
# $ V5: int? 6 3 2 1 2 4
# $ V6: int? 23 28 24 24 40 22
?write.table(mydata,"dataNew.txt",sep=",",row.names=FALSE,quote=FALSE)
##dataNew.txt
V1,V2,V3,V4,V5,V6
1,4,4,1,6,23
1,4,2,2,3,28
1,4,5,1,2,24
1,2,3,1,1,24
1,2,3,1,2,40
1,2,3,1,4,22
#or
?Lines1 <- readLines("data.txt")
?gsub(" +",",",Lines1)
#[1] "1,4,4,1,6,23" "1,4,2,2,3,28" "1,4,5,1,2,24"
"1,2,3,1,1,24" "1,2,3,1,2,40"
#[6] "1,2,3,1,4,22"
A.K.
Hi every one,
I have a text file like this:
1??? 4?? 4??? 1??? 6 ?? 23
1?? 4??? 2??? 2??? 3??? 28
1??? 4??? 5??? 1??? 2??? 24
1??? 2??? 3??? 1??? 1??? 24
1?? 2??? 3??? 1??? 2?? ? 40
1?? 2?? 3??? 1??? 4? ? ? 22
I want to separate columns by comma, like this:
1,4,4,1,6,23
1,4,2,2,3,28
1,4,5,1,2,24
1,2,3,1,1,24
1,2,3,1,2,40
1,2,3,1,4,22??
I used this code:
mydata=read.table("data.txt",sep=",")
but I see this output:
1\t4\t4\t1\t6\t231\t4\t2\t2\t3\t28
...?
please correct my code
Thanks in advance
Elham