I have a List data.list with 5 columns, which looks like this:
Code Price_old MB Price_new Product
CZ 898.00 20.00 1.001.00 Type 1
CZ 890.00 300.00 1.016.33 Type 2
CZ 890.00 1.000.00 1.016.63 Type 2
CZ 899.00 200.00 1.019.33 Type 2
NO 999.00 50.00 1.025.75 Type 3
NO 999.00 600.00 1.025.75 Type 3
This is directly imported from a .csv. What I want to know is a way to convert columns 2, 3 and 4 from factor to numeric (as.numeric(levels(f))[f] did not work!) (1 and 5 are character) without losing any information.
Conversion with mutate_if(is.factor, as.numeric) ended up losing all decimal points: 1.025.75 -> 102575, 50.00 -> 5000, etc.
Conversion with sapply
indx <- sapply(data.list, is.factor)
data.list[indx] <- sapply(data.list[indx],
function(x) as.numeric(as.character(x)))
produced roughly 200 NAs by coercion in each column of my full dataset, data I can not do without.
Second, I want to find a solution to convert all numeric values to this format: "####.##".
I searched in many related blogs and posts, but did not find a proper solution to my problem. Hope someone has an ace up the sleeve.
Cheers
awkand friends? - vaettchenawk- Luke666readrpackage or maybefreadfromdata.tableor the solution from Benjamin. - phiver