Converting character variables to numeric variables

Hello.

I've tryed to convert a chr variable into a Num column and the output was the folllowing:

F1_2016_2018$VFN<-as.numeric(F1_2016_2018$VF)
Warning message:
NAs introduced by coercion

Can you help me avoid this error?

Thank you.

It's not an error, just a warning that some of your values have been converted to NA.

Thank you martin.R but all the column was converted to NA instead of numbers.

That wasn't what I wanted to do.

Ok, in that case all your entries contained one or more non-numeric characters, so they cannot be converted using as.numeric().

Please provide a reproducible example:
FAQ: How to do a minimal reproducible example ( reprex ) for beginners - meta / Guides & FAQs - RStudio Community

martin.R

Yes they have commas as you can see here:

ANO_REF MES_REF FLUXO NPC PAIS_COD NC8 VF ML US

1 2016 1 1 106909045 ES 19053199 2211,00 3955,00 0,00
2 2016 1 1 106909045 ES 20052020 2124,00 401,00 0,00
3 2016 1 1 106909045 ES 23091090 888,00 1090,00 0,00
4 2016 1 1 106909045 ES 30051000 94,00 130,00 0,00
5 2016 1 1 106909045 ES 33072000 58,00 67,00 0,00
6 2016 1 1 106909045 ES 33073000 950,00 1113,00 0,00
7 2016 1 1 106909045 ES 34011900 4543,00 5738,00 0,00
8 2016 1 1 106909045 ES 48181010 1599,00 1837,00 0,00
9 2016 1 1 106909045 ES 48183000 1915,00 2201,00 0,00
10 2016 1 1 106909045 ES 85392198 1507,00 1731,00 3600,00

How can I avois that error?

I cannot tell how your data is formatted from that.

Please refer to the link I posted and use dput() to post example data.

Martin.R

The problem occurs with cells like those that are yellow in this link

Data Error

I'm not clicking on an unknown link. Please follow the instructions I have given to post the data on this forum. Otherwise you will have to rely on somebody else stepping in.

martin.R

The output of dput is this one:

structure(list(ANO_REF = c(2016L, 2016L, 2016L, 2016L, 2016L),
MES_REF = c(1L, 1L, 1L, 1L, 1L), FLUXO = c(1L, 1L, 1L, 1L,
1L), NPC = c(106909045L, 106909045L, 106909045L, 106909045L,
106909045L), PAIS_COD = c("ES", "ES", "ES", "ES", "ES"),
NC8 = c(19053199L, 20052020L, 23091090L, 30051000L, 33072000L
), VF = c("2211,00", "2124,00", "888,00", "94,00", "58,00"
), ML = c("3955,00", "401,00", "1090,00", "130,00", "67,00"
), US = c("0,00", "0,00", "0,00", "0,00", "0,00")), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))

If the comma is intended to be a decimal separator, then you can fix this when importing the data, e.g. via ?read.csv and the dec parameter.

Otherwise this should fix it in the session:

F1_2016_2018$VF <- as.numeric(gsub(",", ".", F1_2016_2018$VF))

Thank you very much martin.R!

You solved my problem.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.