r - Remove thousand's separator -


this question has answer here:

i imported excel file , got data frame this

structure(list(a = structure(1:3, .label = c("1.100", "2.300",  "5.400"), class = "factor"), b = structure(c(3l, 2l, 1l), .label = c("1.000.000",  "500", "7.800"), class = "factor"), c = structure(1:3, .label = c("200",  "3.100", "4.500"), class = "factor")), .names = c("a", "b", "c" ), row.names = c(na, -3l), class = "data.frame") 

i convert these chars numeric or integer. however, dot character (.) not decimal sign "thousand's separator" (it's german).

how convert data frame properly?

i tried this:

df2 <- as.data.frame(apply(df1, 2, gsub, pattern = "([0-9])\\.([0-9])", replacement= "\\1\\2"))  df3 <- as.data.frame(data.matrix(df2)) 

however, apply seems convert each column list of factors. can maybe prevent apply doing so?

you can use :

sapply(df, function(v) {as.numeric(gsub("\\.","", as.character(v)))}) 

which gives :

              b    c [1,] 1100    7800  200 [2,] 2300     500 3100 [3,] 5400 1000000 4500 

this give matrix object, can wrap data.frame() if wish.

note columns in original data not characters factors.


edit: alternatively, instead of wrapping data.frame(), can result directly data.frame:

# as.character(.) in case it's loaded factor df[] <- lapply(df, function(x) as.numeric(gsub("\\.", "", as.character(x)))) 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -