Quantcast

Import in R with White Spaces

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Import in R with White Spaces

francy
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Import in R with White Spaces

Sarah Goslee
Hi,

On Mon, Oct 3, 2011 at 11:14 AM, francy <[hidden email]> wrote:

> 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?

Since you're exporting from Excel, make sure that quoting is turned on in
the export options. That way an entire value, including white space, will
be in quotes and thus read as an entire value. That should have been
done by default, and specifying sep as you did should have worked.

So actually, we may need more information beyond what you've provided.

Sarah

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Import in R with White Spaces

trinker
In reply to this post by francy

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: [hidden email]
> To: [hidden email]
> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Import in R with White Spaces

francy
Ok I added quoting and it did work...Not sure why, but thank you for both
your replies!
-f

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Loading...