editing import data, strings

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

editing import data, strings

gaja
This post was updated on .
Regards.

I'm a beginner in programing, so I have a basic question for you.
If someone could help me please..

I want to create a function, which will be able to import files from excel.
I tried with
a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE),

.. but instead of numbers, it gives me strings for example: "299,311".

I can handle this string for example:
b <- "299,311"
as.numeric(gsub(",", "", b))
299311

Now, I´m interested how to inport it from that file,.

I tried with
a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE)
a <- gsub(",", "", a)
a <- as.numeric(a)

But it doesn't work.
I used search engine on forum, but didn't find any function that I could help with.

I would be very gratefull if someone could help me.

Gaja
Reply | Threaded
Open this post in threaded view
|

Re: editing import data, strings

jholtman
At least post the first 10 lines of the file so that we can see what
it is.  It may be that some of the data is enclosed in quotes, but it
is hard to tell without seeing the actual data.

On Fri, Feb 17, 2012 at 12:24 PM, gaja <[hidden email]> wrote:

> Regards.
>
> I'm a beginner in programing, so I have a basic question for you.
> If someone could help me please..
>
> I want to create a function, which will be able to export files from excel.
> I tried with
> a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE),
>
> .. but instead of numbers, it gives me strings for example: "299,311".
>
> I can handle this string for example:
> b <- "299,311"
> as.numeric(gsub(",", "", b))
> 299311
>
> Now, I´m interested how to inport it from that file,.
>
> I tried with
> a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE)
> a <- gsub(",", "", a)
> a <- as.numeric(a)
>
> But it doesn't work.
> I used search engine on forum, but didn't find any function that I could
> help with.
>
> I would be very gratefull if someone could help me.
>
> Gaja
>
> --
> View this message in context: http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4397899.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

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

Re: editing import data, strings

Michael Weylandt
In reply to this post by gaja
If you know all your data should be numeric, you could perhaps try
something like this:

apply(a, 2, function(x) as.numeric(gsub(" ", "", x)))

but it can't be tested without your actual data. (Look at dput() for
the best way to send data by email)

Michael

On Fri, Feb 17, 2012 at 12:24 PM, gaja <[hidden email]> wrote:

> Regards.
>
> I'm a beginner in programing, so I have a basic question for you.
> If someone could help me please..
>
> I want to create a function, which will be able to export files from excel.
> I tried with
> a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE),
>
> .. but instead of numbers, it gives me strings for example: "299,311".
>
> I can handle this string for example:
> b <- "299,311"
> as.numeric(gsub(",", "", b))
> 299311
>
> Now, I´m interested how to inport it from that file,.
>
> I tried with
> a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE)
> a <- gsub(",", "", a)
> a <- as.numeric(a)
>
> But it doesn't work.
> I used search engine on forum, but didn't find any function that I could
> help with.
>
> I would be very gratefull if someone could help me.
>
> Gaja
>
> --
> View this message in context: http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4397899.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

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

Re: editing import data, strings

gaja
This post was updated on .
Thanx for posting. :)  I wan't to get numbers instead of strings in table.

I'm posting a link to excel file, same as I want import to table.
Its dl link, don't be mad, hehe
http://spreadsheets.google.com/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=csv

Thanx,G.


ps: @Michael Weylandt;
Thanx for your code,... I tried to used it, but unsucsesfully. I really don't know what the "x" stands for.

I hope I will be able to solve this problem.
Gaja
Reply | Threaded
Open this post in threaded view
|

Re: editing import data, strings

jholtman
Is this what you want to do:  this will remove the commas and convert to numeric


> x <- read.csv("C:\\jph\\indicatorgapminderpopulation.csv"
+             , as.is = TRUE
+             , check.names = FALSE
+             )
>
> # convert to numeric column 2+
> for (i in 2:ncol(x)){
+     x[, i] <- as.numeric(gsub(",", "", x[, i]))
+ }
>     str(x)
'data.frame':   259 obs. of  233 variables:
 $ Total population: chr  "Abkhazia" "Afghanistan" "Akrotiri and
Dhekelia" "Albania" ...
 $ 1700            : num  NA NA NA 300000 1750000 ...
 $ 1730            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1750            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1785            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1786            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1787            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1788            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1789            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1790            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1791            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1792            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1793            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1794            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1795            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1796            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1797            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1798            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1799            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ 1800            : num  NA 3280000 3710 410445 2503218 ...
 $ 1801            : num  NA NA NA NA NA NA NA NA NA NA ...



On Fri, Feb 17, 2012 at 1:43 PM, gaja <[hidden email]> wrote:

> Thanx for posting. :)
>
> I'm posting a link to excel file, same as I want import to table.
> Its dl link, don't be mad, hehe
> http://spreadsheets.google.com/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=csv
>
> Thanx,G.
>
>
> ps: @Michael Weylandt;
> Thanx for your code,... I tried to used it, but unsucsesfully. I really
> don't know what the "x" stands for.
>
> I hope I will be able to solve this problem.
> Gaja
>
> --
> View this message in context: http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4398120.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

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

Re: editing import data, strings

Henrique Dallazuanna
In reply to this post by gaja
Try this:

setClass("myClass")
setAs("character", "myClass", function(from)as.numeric(gsub(",", "", from)))

d <- read.table("clipboard", sep = ",", check.names = FALSE, quote = '"',
header = TRUE, fill = TRUE, colClasses = c('character', rep('myClass',
232)))

On Fri, Feb 17, 2012 at 3:24 PM, gaja <[hidden email]> wrote:

> Regards.
>
> I'm a beginner in programing, so I have a basic question for you.
> If someone could help me please..
>
> I want to create a function, which will be able to export files from excel.
> I tried with
> a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE),
>
> .. but instead of numbers, it gives me strings for example: "299,311".
>
> I can handle this string for example:
> b <- "299,311"
> as.numeric(gsub(",", "", b))
> 299311
>
> Now, I´m interested how to inport it from that file,.
>
> I tried with
> a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE)
> a <- gsub(",", "", a)
> a <- as.numeric(a)
>
> But it doesn't work.
> I used search engine on forum, but didn't find any function that I could
> help with.
>
> I would be very gratefull if someone could help me.
>
> Gaja
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4397899.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

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

Re: editing import data, strings

Michael Weylandt
In reply to this post by gaja
It looks like you've gotten answers elsewhere, but for completeness, I'll explain my shot in the dark:

The construction

function (x) sin(x^2)

to pick one example, is what's called an anonymous (or lambda) function. In R, they are often used in conjunction with the *apply() family to describe a set of operations to be done column/rowwise on your data. For example, there are the same:

apply(d, 2, sin)
apply(d, 2, function(x) sin(x))
apply(d, 2, function(y) sin(y))
apply(d, 2, function(d) sin(d))

As you can see, the "x" is simply a place holder variable, not really tied to anything.

Hope this helps,
Michael

On Feb 17, 2012, at 1:43 PM, gaja <[hidden email]> wrote:

> Thanx for posting. :)
>
> I'm posting a link to excel file, same as I want import to table.
> Its dl link, don't be mad, hehe
> http://spreadsheets.google.com/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=csv
>
> Thanx,G.
>
>
> ps: @Michael Weylandt;
> Thanx for your code,... I tried to used it, but unsucsesfully. I really
> don't know what the "x" stands for.
>
> I hope I will be able to solve this problem.
> Gaja
>
> --
> View this message in context: http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4398120.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

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