1
whereas the content of a certain variable is "001"
class Character.
I need to transform that character to numeric. If I do as.numeric()
, he returns me 1
.
I want you to return me exactly 001, is it possible?
Thank you.
1
whereas the content of a certain variable is "001"
class Character.
I need to transform that character to numeric. If I do as.numeric()
, he returns me 1
.
I want you to return me exactly 001, is it possible?
Thank you.
4
The problem is in ==
and not in class. The ==
works only for one value. To compare with more than one value, it has to be %in%
. In your case:
desp2[desp2$codigo_novo %in% c('1049', '1001'),]
Similarly in the subset:
subset(desp2, codigo_novo %in% c('1049', '1001'))
Using the ==
, two comparisons would be necessary:
desp2$codigo_novo == '1049' | desp2$codigo_novo == '1001'
What if I want to use a sequence of codes? For example, I need to select the lines whose codes are continuously from "1049" to "1080" ... How I would avoid writing code by code?
Da to use codigo_novo %in% 1049:1090
(1049:1090 returns a vector with the integers between 1049 and 1090). Although it is a number, it will be transformed into a string at the time of comparison. Note that it will only work because the number has 4 characters, if you have less you will have to use the sprintf()
to add the 0 to the left (Ex: sprintf('%04d', c(1, 10, 100, 1000))
)
Browser other questions tagged r
You are not signed in. Login or sign up in order to post.
001 is not exactly a number, maybe you should work with text yourself. If you tell me what you want to achieve that you need 00 up front maybe somebody can help you.
– Math
In fact, I have a database where one of the columns calls "Code" . In this column I have characters like this. For example, "00010" or "01234". I need to select from this database the lines that have certain codes.. for example, I want to select only the code lines "00010" and "00020". When I do something like desp2<- desp2[ desp2$codigo == "00020" | "00020",] give me an error, because this logical operator does not work with characters. Hence my need to convert to numeric
– orrillo
It’s best to work with characters.
– Jorge B.
I modified my previous comment... now I think it’s clearer what I want
– orrillo
How’s the type of your "Code" field in your BD? If it’s not numerical it won’t do you much good to search with numbers.
– Math
Good afternoon @Vasco, but it would not be better to adapt the database by changing the column to numeric?
– Leonardo Bosquett
Vasco, the
==
should be functioning normally, even ifcharacter
. The problem is probably something else, if you put an example of the data and the code becomes easier to help, abs.– Carlos Cinelli
I want to turn that column into numeric. I had a sun, I added digit 1 in all rows of the code column as first digit. So, it’s time to turn Character to numeric, I don’t lose its original location.But even so, at the time I desp2<- desp2[ desp2$codigo_novo == 1049 | 1001,] it seems that the selection is not successful, because the number of lines ends up being the same. On the other hand, when I desp2<- desp2[ desp2$codigo_new == 1001,] the selection is successful, because the number of lines is smaller.
– orrillo
Guys, @Carloscinelli .. I managed to solve my problem using the subset function even with characater... thanks guys
– orrillo