[R] editing import data, strings

jim holtman jholtman at gmail.com
Sat Feb 18 01:10:19 CET 2012


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



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



More information about the R-help mailing list