Posts

Showing posts with the label subset

R Extract values from data frame with vectors

Image
R Extract values from data frame with vectors I hope my question is not a duplicate, but I really couldn't find what I need. I dived in dplyr and even did the data.table course from DataCamp, but I can't fix it. Maybe overlooking something? I have this df: set.seed(1964) df<-data.frame(id = c( "XEY", "NZH", "DYE", "JNF", "LHH", "WNB"), q_1 = sample(5,6, replace = TRUE), q_2 = sample(5,6, replace = TRUE), q_3 = sample(5,6, replace = TRUE), q_4 = sample(5,6, replace = TRUE), q_5 = sample(5,6, replace = TRUE)) So my df looks like this: Next I have two vectors with id's (row-indicators) and q's (question numbers) like these: id <- c("XEY", "DYE", "JNF", "DYE", "XEY", "LHH", "WNB", "JNF" ) question <- c("val_1", "val_1","val_3","val_3"...

subset a dataframe based on a matrix of row numbers and save the result in one list

subset a dataframe based on a matrix of row numbers and save the result in one list I have a data frame called df that looks like: > df Date A B C 1 2001 1 12 14 2 2002 2 13 15 3 2003 3 14 16 4 2004 4 15 17 5 2005 5 16 18 6 2006 6 17 19 7 2007 7 18 20 8 2008 8 19 21 9 2009 9 20 22 10 2010 10 21 23 and a matrix called index that looks like: > index Resample01 Resample02 Resample03 Resample04 Resample05 [1,] 1 7 1 2 7 [2,] 3 9 2 3 8 [3,] 5 1 3 8 1 [4,] 8 3 4 9 4 [5,] 10 4 5 10 9 The numbers in each column stands for the row number to be selected. The aim is to split the dataframe into two exclusive groups of "train" and "test" according to the row numbers in each column of the matrix "index". For exampl...