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 ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.