Thanks, I will do that. On Tue, Oct 20, 2020 at 4:15 PM Jeff Newmiller <jdnew...@dcn.davis.ca.us> wrote: > > 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.lu...@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.
-- 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.