Skip to content

editing import data, strings

7 messages · gaja, R. Michael Weylandt, jim holtman +2 more

#
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.
#
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 <gajahorvat at hotmail.com> wrote:

  
    
#
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 <gajahorvat at hotmail.com> 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.
#
Is this what you want to do:  this will remove the commas and convert to numeric
+             , as.is = TRUE
+             , check.names = FALSE
+             )
+     x[, i] <- as.numeric(gsub(",", "", x[, i]))
+ }
'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 <gajahorvat at hotmail.com> wrote:

  
    
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 <gajahorvat at hotmail.com> wrote: