Reputation: 1
My goal is to create a data frame of 3 Variables (gender, Age and duration of employment) with the sample function. Here ist what i wrote already:
x <- data.frame(Geschlecht = sample(c("m","w"),10,replace=TRUE), Alter = sample(18:68,10,replace=TRUE), Dauer = sample(1:12,10,replace=TRUE))
My problem: The Duration should be bounded by the age of an employe. For example: When an employe is 20 the duration of employment should - obvious - not be longer than 2 years. How can i solve that?
Upvotes: 0
Views: 162
Reputation: 887741
In base R
, we can also do this without creating a column
subset(x, Alter - Dauer >= 18)
Upvotes: 0
Reputation: 7413
Here's a solution in base R
# Create a flag with your parameters
x$flag <- ifelse(x$Alter - x$Dauer < 18, TRUE, FALSE)
# If you want to filter out any results
x <- subset(x, flag == FALSE)
# Delete flag variable
x$flag <- NULL
Here's a simpler solution using dplyr
, assuming you want to filter out any invalid results.
library(dplyr)
x <- x %>%
filter(Alter - Dauer >= 18)
Result:
Geschlecht Alter Dauer
1 w 23 3
2 w 62 2
3 m 61 10
4 m 45 9
6 w 56 3
7 m 64 10
8 w 25 1
9 m 60 5
10 w 63 11
Upvotes: 1