Question: I want to replace the missing value for male in class 2 with the media
ID: 2246416 • Letter: Q
Question
Question: I want to replace the missing value for male in class 2 with the median age 27.5, the missing value for female in class 2 with 40, and the missing value for male in class 3 with 26. How can I do that in R Studio?
median age for male in class 2 is 27.5
median age for female in class 2 is 40
median age for male in class 3 is 26
age <- c(34, 26, 21, NA, NA, 40, 22, 33, NA, 45)
gender <- c(1, 2, 1, 1, 1, 2, 2, 1, 2, 1) # 1=male, 2=female
class <- c(2, 3, 2, 2, 3, 2, 3, 1, 2, 1)
df <- data.frame(age, gender, class)
age
<dbl>
gender
<dbl>
class
<dbl>
age
<dbl>
gender
<dbl>
class
<dbl>
1 34 1 2 2 26 1 3 3 21 1 2 4 NA 1 2 5 NA 1 3 6 40 2 2 7 22 2 3 8 33 1 1 9 NA 2 2 10 45 1 1Explanation / Answer
In programming languages, storing some kind of data into the memory of the computer (in fact into the current symbolic workspace dened by your R session) is called assigning a value to a variable. When you assign a value (whatever its type: it can be a string of characters, an integer, a boolean variable, a oating-point number, etc) to a variable, you create a new object with R. This is done through the assignment operator ->. This operator is written with a hyphen (-) followed by a greater than sign (>), so that it forms an arrow. Taking care of swapping the two operands (variable and value), this can be also written the other way around: > 5 -> a # puts the value 5 into the variable a > b <- 7 # puts the value 7 into the variable b > b - a # asks for the evaluation (calculation) of b-a [1] 2 > 2 # nothing prevents you from evaluating a constant! [1] 2 The greater than character at the beginning of a commandline is the invite. It means that R is waiting for you to enter a command. Some commands produce a silent output (e.g. assigning a value to a variable is a silent operation), some other display a result. For example, subtracting the content of the variable called a from the content of the variable called b gives the single value 2 as an output. Notice that the [1] automatically prepended to the output line is an index: as R tends to see everything as a vectorized variable (you will see other examples very soon), here it tells us that this 2 is the rst element of the output vector produced by the operation b-a (which contains only one element). On the last line of the above interaction, you can see that even typing a mere value and asking R to evaluate it by pressing the key yields the exact same output with the exact same formatting. 4 1.3 Vectors and function calls As we just said, vectors are essential data structures in R. One creates vectors simply by using the concatenation operator (or function), rightfully named c: > myvec <- c(1,3,5,7,84) # passing five integers as arguments to the function c > myvec # myvec is now a vector containing five elements [1] 1 3 5 7 84 > length(myvec) # calling the function length on the object myvec [1] 5 Here we have just used our rst two functions in R. With the rst command of this interaction block, we performed a function call to the function called c, giving it ve integers as arguments. A function call in R is always written as this: the name of the function is followed by a pair of parentheses enclosing a list of comma-separated arguments (possibly only one or even none). Hence, the last operation we performed above is a function call to the function length, passing to it one argument only, namely the myvec variable. Remember that whenever you want to use a function, you have to write these parentheses. Even if the function needs no argument. For instance, the function ls, when invoked (another vocable computer people use for called) with no argument, lists the content of your current userspace or environment: it returns a vector populated with strings giving the names of these objects present in your current R environment (i.e. the variables you dened earlier in your interactive session). If you omit the parentheses, though, R will try and evaluate the function itself : > ls() [1] "a" "b" "myvec" > ls function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE, .... some complex output clipped off ....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.