i have uploaded file,but when i am opening it in R,using> u<-file(file.choose(),"r") >k<-readLines(u) >k > k[1:120]is has all /t (tabs)& newlines, how to avoid it, can i take first 3 columns only in table form (lines starts with # not important for me) uploaded file:- http://r.789695.n4.nabble.com/file/n4577757/rabata.txt rabata.txt -- View this message in context: http://r.789695.n4.nabble.com/how-to-avoid-newlines-tabs-in-file-opening-tp4577757p4577757.html Sent from the R help mailing list archive at Nabble.com.
Hello, sagarnikam123 wrote> > i have uploaded file,but when i am opening it in R,using > >> u<-file(file.choose(),"r") >>k<-readLines(u) >>k >> k[1:120] > > is has all /t (tabs)& newlines, how to avoid it, > can i take first 3 columns only in table form (lines starts with # not > important for me) > > uploaded file:- > http://r.789695.n4.nabble.com/file/n4577757/rabata.txt rabata.txt >Try the following. # Read from the link you gave fl <- file("http://r.789695.n4.nabble.com/file/n4577757/rabata.txt", "rb") bin <- readBin(fl, what="character") close(fl) # Get rid of tabs and '\r' if any bin <- gsub("[[:blank:]]", " ", bin) bin <- gsub("\\r", "", bin) # Split in lines of text and keep those not starting with '#' txt <- unlist(strsplit(bin, "\\n")) txt <- txt[substr(txt, 1, 1) != "#"] # Now make a data.frame of it, cols 1:3 only lst <- lapply(strsplit(txt, " "), function(x) x[1:3]) df1 <- data.frame(do.call(rbind, lst), stringsAsFactors=FALSE) # See what we have str(df1) head(df1) # And revamp col 1 df1$X1 <- as.integer(df1$X1) str(df1) head(df1) # Final clean-up # rm(bin, txt, lst) Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/how-to-avoid-newlines-tabs-in-file-opening-tp4577757p4578360.html Sent from the R help mailing list archive at Nabble.com.
How about, "don't avoid them, use them"? dta <- read.table( " http://r.789695.n4.nabble.com/file/n4577757/rabata.txt rabata.txt", as.is=TRUE, skip=4, sep="\t" ) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. sagarnikam123 <sagarnikam123 at gmail.com> wrote:>i have uploaded file,but when i am opening it in R,using > >> u<-file(file.choose(),"r") >>k<-readLines(u) >>k >> k[1:120] > >is has all /t (tabs)& newlines, how to avoid it, >can i take first 3 columns only in table form (lines starts with # not >important for me) > >uploaded file:- >http://r.789695.n4.nabble.com/file/n4577757/rabata.txt rabata.txt > >-- >View this message in context: >http://r.789695.n4.nabble.com/how-to-avoid-newlines-tabs-in-file-opening-tp4577757p4577757.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
Seemingly Similar Threads
- how to append element at last position in array dynamically
- how to avoid writing index in write.table command
- gzfile & read.table on Win32
- Is it there any std pattern in R which show same representation style of ppt found on net
- how to check given number seq. is time series or not?