I use the following function I stole somewhere.  There's probably better ways.
 
white <- function(x){
x <- as.data.frame(x)
W <- function(x) gsub(" +", "", x)
sapply(x,W)
}

#EXAPLE
dat <- paste(letters," ", " ", LETTERS)
(DAT <- data.frame(dat, dat))   #nasty white spaces
white(DAT)   #white spaces gone
 
 
Tyler
 

> Date: Mon, 3 Oct 2011 08:14:27 -0700
> From: francy.casal...@gmail.com
> To: r-help@r-project.org
> Subject: [R] Import in R with White Spaces
> 
> Hi,
> 
> I have a simple question about importing data, I would be very grateful if
> you could help me out.
> 
> I have used read.csv(file name, header=T, sep=",") to bring in a csv file I
> saved in MS Excel.The problem is I have white spaces in the middle of values
> (not in the column names), and this messes up the column entries. Since I
> have many many files that I am importing and I have spaces in all of them, I
> was looking for a way to avoid going into all of them and changing the white
> spaec to, for example, an underscore.
> Can you suggest whether there is a way to tell R that each element delimited
> by "," is actually a different entry, regardless of whether there are white
> spaces in between?
> 
> Thank you so much for the help!
> -f
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Import-in-R-with-White-Spaces-tp3867799p3867799.html
> Sent from the R help mailing list archive at Nabble.com.
> [[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]]

______________________________________________
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.

Reply via email to