How to assign the same levels to a dataframe column?
Stop using as.factor() and start using factor()? Be explicit about what levels you want and in what order. As for extracting subsets, use split() and do your subsets on each set of rows with the same level. It will be on you to decide what statistical properties (proportions?) you want to maintain between the full data set and your subset.
On October 20, 2020 6:42:27 AM PDT, Luigi Marongiu <marongiu.luigi at gmail.com> wrote:
Hello, I have a dataframe df with a column x that has these unique values: "L" "M" "V" "N" "H". I can assign a factor to it: ``` df$x = as.factor(df$x)
[1] L M V N H
Levels: H L M N V ``` I now need to get a subset of this dataframe. I could do the same thing as before on the subset sf, but I would like to avoid the possibility that not all the values could be present in sf. For instance, sf could have only the values "M" "V" "N". How can I assign a given set of value to the subset dataframe? Now I am getting the error: ``` LEV = as.factor(df$x)
LEV
[1] L M V N H Levels: H L M N V
sf$x <- LEV
Error in `$<-.data.frame`(`*tmp*`, x, value = c(2L, 2L, 2L, 2L, 2L, : replacement has 7300 rows, data has 117
LEV = as.factor(unique(df$x)) LEV
[1] L M V N H Levels: H L M N V
sf$x <- LEV
Error in `$<-.data.frame`(`*tmp*`, Type, value = c(2L, 3L, 5L, 4L, 1L)) : replacement has 5 rows, data has 29 # NOTE: here sf was another subset) ``` Thank you
Sent from my phone. Please excuse my brevity.