In R: remove commas from the field AND change the modified field in the data frame part

I need to remove commas from a field in an R-frame. Technically, I managed to do this, but the result does not seem to be either a vector or a matrix, and I cannot return it to the framework in a convenient format for use. So, there is a way to remove commas from the field, And this field will remain part of the data frame.

Here is an example of a field for which you want to remove commas and the results generated by my code:

> print(x['TOT_EMP'])
         TOT_EMP
1    132,588,810
2      6,542,950
3      2,278,260
4        248,760

> y
[1] "c(\"132588810\" \"6542950\" \"2278260\" \"248760\...)"

The desired result is a number field:

       TOT_EMP
1    132588810
2      6542950
3      2278260
4       248760

x<-read.csv("/home/mark/Desktop/national_M2013_dl.csv",header=TRUE,colClasses="character")
y=(gsub(",","",x['TOT_EMP']))
print(y)
+4
source share
1 answer

gsub() , ( , ). as.numeric() :

> df <- data.frame(numbers = c("123,456,789", "1,234,567", "1,234", "1"))
> df
      numbers
1 123,456,789
2   1,234,567
3       1,234
4           1
> df$numbers <- as.numeric(gsub(",","",df$numbers))
> df
    numbers
1 123456789
2   1234567
3      1234
4         1

- data.frame:

> class(df)
[1] "data.frame"
+7

All Articles