I am struggling with a subset function and hope that I can get some help from the cloud.
Within my dataset surveydata
one can find the column landscape
. I need to both select all landscapes of type 7
and 5
and to randomly select 50
objects from each landscape type 3
and 6
. Then I want to create a new variable called sub
in the surveydata
dataframe which should contain a number e.g. 1
if the object was selected in the previous selection and 0
(or NA
) if it wasn't.
Preferably I search a base R solution, but I don't stick to that.
I provide a random dataset for better understanding.
#create data
surveydata <- as.data.frame(replicate(6,sample(0:1,1000,rep=TRUE)))
# change values of columns
surveydata$V3 <- sample(7, size = nrow(surveydata), replace = TRUE)
surveydata$V4 <- sample(5, size = nrow(surveydata), replace = TRUE)
surveydata$V5 <- sample(5, size = nrow(surveydata), replace = TRUE)
surveydata$V6 <- sample(5, size = nrow(surveydata), replace = TRUE)
#create column with same distribution of values
surveydata$group <- c(1,2)
# rename columns
colnames(surveydata)[1] <- "gender"
colnames(surveydata)[2] <- "expert"
colnames(surveydata)[3] <- "landscape"
colnames(surveydata)[4] <- "q1"
colnames(surveydata)[5] <- "q2"
colnames(surveydata)[6] <- "q3"