Psy 202 CA2
Psy 202 CA2
1. What is R?
R is an object-oriented language used to analyze and visualize data. It treats informa on as
objects— that are mostly numbers. It is used for sta s cal compu ng and analyses. It is domain-
speci c and a very popular sta s cal so ware. It can be installed on a for convenient use.
2. Dr. Navarro iden ed ve bene ts of using R to teach sta s cs. What were those ve
bene ts?
1. Using R on the computer is faster, easier, and more e cient. The computer performs well at
repe ve tasks and sta s cs.
2. Using R rather than a spreadsheet like Microso Excel allows you to have ample opportuni es
to perform sta s cs. Excel is limited in the types of analyses it can do.
3. Rather than se ling for an expensive student package of so ware, R is an open source and is
also free to everyone.
4. R is highly extensible in that it has become a standard tool as it is used by many people. R is
used in advanced textbooks. Learning and using R would be bene cial in helping to
understand other sta s cal analyses that also use R.
5. As programming is a core research skill used in various social and behavioral sciences, learning
R is a great way to start learning sta s cs as it is a real programming language. Anyone “doing
research in psychology” would nd using R to be very bene cial.
8. R can be used to do simple calcula ons. Describe in words what the following lines of
code tell R to do:
10 ^ 2 The code is telling R to nd the answer of 10 squared, or to the second power, and the
product would be 100.
11. We can assign values to variables in R using the -> and <- assignment operators.
Describe what the following lines of code tell R to do:
sheep <- 45 The code is telling R to take the value of whatever is behind that arrow and
assign it to whatever is in front of the arrow, “sheep” would be assigned to “45”.
50 -> clone The code is telling R to take the value of what is on the le and assign it to
the value on the right, meaning “50” would be assigned to the “clone”.
clone <- sheep The code tells R to change the value of “sheep” and assign it to “clone”
leading to the value of “cone” being 45, rather than 50.
12. R can perform opera ons on variables as well as on individual values. If we tell R to
run the code sheep <- 45 and then run 50 -> clone, what does it do when we
tell it to run sheep + clone?
ft
ti
fi
fi
fi
ti
ti
ti
ff
ti
fi
ti
fi
ti
ti
ti
ti
ti
ti
ti
ti
ti
ft
ti
fi
ft
fi
ft
ti
When we tell R to run “sheep + cone,” R will perform an addi on equa on using 45 and 50 to
return 95 as the answer.
13. Like all programming languages, R has formal rules and informal conven ons for
naming variables. What are ve formal rules that Dr. Navarro iden ed?
1. Variable names can only use uppercase and lowercase le ers along with the numbers 0-9 in
the name. You can also use periods (.) or underscores (_), but no other characters. For
example, D_og.s works as a name but dogs! does not.
2. Spaces cannot be used in variable names. Ex: GO REBS is not valid, but GO_REBS is.
3. The names are case-sensi ve— DOGS and Dogs are di erent.
4. Variable names can only start with a le er or a period, but star ng with a period (.) is not
always conven onal. “_cats” is not a variable name.
5. Reserved keywords cannot be used as a variable name. Key words including “if, else, repeat,
func on, in, break, FALSE, TRUE, Inf, NA, while, for, next, NaN, NA_interger_, NA_complex_,
NA_real_, and NA_character_” cannot be used.
What are three informal conven ons that Dr. Navarro iden ed?
1. Use informa ve variable names— using meaningful names is easier to understand and keep
up with.
2. Use short variable names to avoid painful long names that take up space and me.
3. Use a conven onal naming style for variables that are mul -words. You can separate words in
mul -word variables by using underscores or periods. You can also use capital le ers to
represent the start of a word.
15. How does RStudio make using func ons easier for programmers?
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
fi
ti
ti
tt
ti
ti
fi
ti
ti
ti
ti
ff
ti
tt
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
fi
ti
ti
tt
ti
Rstudio has autocomplete ability, meaning that, while you type the func on, Rstudio gives you
various op ons of what R might expect you to type, making it easier and quicker. R also
automa cally keeps track of your “command history” which makes it easier to use func ons.
17. Variables in R usually store one of three classes of data. What are: *22 mins in video
…numeric data? Numerical data can be used in mathema cal opera ons: simple calcula ons,
using func ons to do calcula ons (like by using PEMDAS),
…character data? Character data includes le ers, words, and symbols
…logical data? Logical data reports if a statement is true or not. There are many logical data
operators
<= The quan ty on the le is less than or equal to the quan ty on the right
> The quan ty on the le is more than the quan ty on the right.
>= The quan ty on the le is greater than or equal to the quan ty on the right
== This stands for “equal to.” This is the equality operator that forces R to determine if it is true
or false.
!= This stands for “not.” If both statements on the right and le are not equal, this is used.
& Are both the statements on the le and right of the “&” are true
sqrt() This func on nds the square root of the number in the parenthesis.
detach() This is used to remove the a achment in the data framework that was made by
the a ach () func on.
tt
ti
ti
tt
ti