10/13/2023 0 Comments Convert string to integer r![]() ![]() library(collapse)į <- factor(sample(runif(5), 5, replace = TRUE)) ![]() The collapse package includes a wrapper around as.numeric(levels(f)) and as.character(levels(f)) in as_numeric_factor and as_character_factor. Otherwise there is a high chance it will not be possible to get back to them only from a factor. So if you will need the original values, you have to keep them. > orig_levels x1 all.equal(x, x1)Īnd this will work only in case when labels have been defined for all possible values in the original data. If we know the original levels (in correct order), we can revert back to the original values. To revert back to the original values we have to know the values of levels used in creating the factor. We can see that f holds only integer values 1, 2, 3, 4 and two attributes - the list of labels ("A", "B", "C", "D") and the class attribute "factor". I will explain it with an example.Īssume the data is vector x: x typeof(x)Ģ) It is not possible to revert back to the original values (10, 20, 30, 40) having only f available. It is possible only in the case when the factor labels match the original values. "virginica" "virginica" "virginica" "virginica" "virginica" "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" "setosa" "setosa" "setosa" "setosa" "setosa" # check if the last column is correctly converted "numeric" "numeric" "numeric" "numeric" "character" "numeric" "numeric" "numeric" "numeric" "factor" Sepal.Length Sepal.Width Petal.Length Petal.Width Species For example the iris dataset: sapply(iris, class) This example can be a quick start: x "character" The most easiest way would be to use unfactor function from package varhandle which can accept a factor vector or even a dataframe: unfactor(your_factor_variable) However you do the conversion, this operation is unlikely to be the bottleneck in your code, so don't worry too much about it. If the values are mostly unique, there won't be much difference in speed. The speed difference will be most apparent for long vectors with few levels. Why is as.numeric(levels(f)) more efficent than as.numeric(as.character(f))?Īs.numeric(as.character(f)) is effectively as.numeric(levels(f)), so you are performing the conversion to numeric on length(x) values, rather than on nlevels(x) values. Is there a better way to convert a factor to numeric? I have to resort to paste to get the real values: as.numeric(paste(f)) f <- factor(sample(runif(5), 20, replace = TRUE)) When I convert a factor to a numeric or integer, I get the underlying level codes, not the values as numbers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |