0
votes

In R i perform calculation. With the functions i count the number of "Males" But in my dataset I also have "Female" As a job I also five other categories for instance "Nurse"

This is my code

a=sum(mydata$gender == "Male")
b=sum(mydata$job == "Doctor" & mydata$gender == "Male")
ab=b/a

I want to have a function that makes it possible to replace for instance mydata$gender == "Male" with mydata$gender == "a" where "a" is a function that ask me to pick the type of variable that i want. For instance in the gender the R console ask me in mydata$gender to select Female based on the categories in mydata$gender

And then in mydata$job == "b" the R console asks me to pick nurse or cleaner or director based on the categories in the variable job

Is this possible or do you guys know something that looks like this.

Example of dataset:

  • ID, job, gender, day of birth 1, Doctor, Male, 23-04-1980
  • 2, Doctor, Female, 23-04-1985
  • 3, Nurse, Female, 23-04-1983
  • 4, Cleaner, Male, 23-04-1982
  • 5, Director, Male, 23-04-1986
1
If you provide some sample of your data, it is easier to understand the problem... - Edu
R is a programming language designed for interactive use in a console. It doesn't really provide a lot of support for creating UI elements to choose values .. you are just expected to assign values to variables yourself. That said, there are packages designed to create UI layers. For example, shiny was created to provide rich HTML interfaces to R programs. Perhaps that's something you might be interested in. - MrFlick

1 Answers

0
votes
   ID <- c(2,3,4,5)
   Job <- c("Doctor","Nurse","Cleaner","Director")
   Gender <- c("Female","Male")
   DOB <- as.Date(c("1982-04-23","1983-04-23","1984-04-23","1985-04-23"))

   df <- function(a,b,c,d){
   my_df <- data.frame(ID=a,Job=b,Gender=c,DOB=d)
   my_df
   }

   df(ID[3],Job[2],Gender[2],DOB[3])
   #   ID   Job Gender        DOB
   # 1  4 Nurse   Male 1984-04-23

   df(ID[c(1,3)],Job[c(2,2)],Gender[c(1,2)],DOB[c(4,3)])
   #   ID   Job Gender        DOB
   # 1  2 Nurse Female 1985-04-23
   # 2  4 Nurse   Male 1984-04-23