[R] How to assign the same levels to a dataframe column?
Luigi Marongiu
m@rong|u@|u|g| @end|ng |rom gm@||@com
Tue Oct 20 15:42:27 CEST 2020
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
--
Best regards,
Luigi
More information about the R-help
mailing list