General RStudio Tips
Set your working Directory :
setwd()
- ensures all file paths are
correct.
getwd()
– for checking whether the current
directory
Packages and Libraries:
Install packages if needed-
install.packages(“package_name”)
and load
them - library(package_name)
detach(package:"", unload=TRUE)
to detach a package
important to open packages at the beginning of your script.
Data Structures
Data Types: string<-"just a text"
,
numeric<-any numbers
,
integer<-as.integer(5)
, logical<-TRUE
,
logical2<-FALSE
(TRUE=1; FALSE=0)
Basic structures: Vectors, Matrices, Data Frames, and
Lists: - Use
c(), matrix(), data.frame(), and list()
dim()
for checking dimensions
length()
check length for vectors
class()
for checking object types
nrow()
and ncol()
for see number of rows
and columns
assign names using names(), rownames(), colnames()
rbind()
and cbind()
for combining data by row
and column
attributes()
check variable attribute in survey data
(specially using SPSS data in haven)
Data Import and Management
Data Import: Use read_sav()
from the haven
package for SPSS files and read.csv()
for CSV files.
after setting up working directory
Use load()
for .rdata files and
readRDS()
for .rds files.
Use gc()
If your codes are not working it clears memory
without deleting objects.
Use read_excel()
from the readxl package for
Excel files.
Viewing Data: Use View()
to visually see
data frames in RStudio.
Subsetting Data: Use logical conditions inside square
brackets for subsetting data frames,
e.g., data[data$variable == value, ]
.
Data Transformation
Recoding Variables: Use cut()
and
break()
| and recode()
from the car
package recoding tasks | also scale::rescale()
from the scale package for recoding variables
Labeling Variables: Use labelled()
from the haven package to assign labels to
variables.
Creating New Variables: Use ifelse()
to
create new conditional variables efficiently. clearer but complicated
search in class scripts (Class 3)
Handling Missing Data: Use na.rm = TRUE
in
functions to ignore missing values during calculations and handle
NA using na.omit() or
complete.cases(). also is.na
identify
missing values and !is.na
identifies non-missing values
Class 3 for NAs
Descriptive Statistics
Basic Statistics: Use summary()
for a quick
overview and table()
for frequency counts.
sd()
for SD, median()
for med,
mean())
for mean. max()
and min()
for finding max and min values in dataset
Grouped Statistics: Use tapply()
to compute
statistics by group and aggregate()
for more complex
grouping operations.
Descriptive detailed Statistics: Use
Hmisc::describe()
and psych::describe()
for
detailed summaries of data. load packages before if using (refer
Class 5 script)
Cross-tabulations: Use xtabs()
for creating
cross-tabulations, also with table()
Also cumsum()
for finding cumulative sums.
prop.table()
for proportions
addmargins()
for adding totals to tables
quantile()
for calculating quantiles
var()
for variance calculation
Visualization Refer (class 8_visualization scropts)
Tables: Use knitr::kable()
to create
well-formatted tables for reports. use above said xtabs()
for cross-tabulations under this (Class 6)
ggplot2 Basics: Remember the structure:
ggplot(data) + geom_function(mapping = aes(…))
Customizing Plots: Use labs()
,
xlab()
, ylab()
,
scale__continuous()
, and theme(
to customize
axes, labels, and themes.
Histograms and Bar Charts: Use
geom_histogram()
for histogram and geom_bar()
for bar charts.
ggplot2 customizing colour and all: Use
scale_fill_manual()
to customize colors in plots. __Use
alpha in geom_point() to set transparency.
- scale_x_continuous()
and
scale_y_continuous()
for axis customization
theme()
customization options
geom_hline()
, geom_vline()
,
geom_abline()
for adding reference lines
adjusting position parameters in
geoms (like "dodge", "fill")
-
Combining Plots: Use ggpubr::ggarrange()
to
arrange multiple ggplot2 plots in a single figure.
Exporting data or saving file
Exporting Results: Use
writexl::write_xlsx()
to export data frames to Excel and
write.csv2()
for CSV files. (Class 4)
Use ggsave()
for ggplots to save (Class
8)
Useful Functions and Techniques
Indexing: Use $
to access columns by name
in data.frame and []
for indexing cases in list matrices,
vectors etc.
Factor Levels: Use as_factor()
to work with
labeled data, especially when using haven. levels()
for
checking factor levels, and ordered=TRUE
parameter in
factor creation remember - as_factor() vs as.factor()
distinction : as_factor()
is work with labelled
spss/stata(using haven) as.factor()
is basic R function
Factor Handling: Use droplevels()
to remove
unused factor levels in a subset of data.
Rounding: Use round()
to specify the number
of decimal places.
Also rowMeans()
for calculating means across rows
use x:y
as to create sequence from x to y (example) or
seq(x,y)
for sequence from x to y. sequence example
in Class 1
and rep()
replication or repeating values to make
sequence
and sort()
for sorting elements
also sum()
for sum
also range()
shows min and max values in one
command.
And rm(X)
for removing objects,
rm(list=ls())
removing all objects removing in
global environment
then ls()
objects()
print
,
show
, print_labels
(in haven-imported
data)
remember usage of Syntax Elements ""
, ''
,
:
, ;
and |
and &
in subsetting difference.
Use help()
, ?
help, and
example()
to understand functions and resolve issues and
use R as calculator if needed (Class
1) sqrt() to see square root
^ for power. Use options(scipen=999)
to
switch off strange numbers.
Data Checking:
Use head()
, tail()
, str()
, and
summary()
frequently to inspect data and ensure it.
Interpretation
In interpretation do include
a conclusion
and
indicate trends and patterns
Example by ma'am from class 9
: In Russia
majority of respondents are religious (61%) and minority (8.3%) claim
that they are atheists. Third of Russian sample answered that they are
not religious (30.6%). In Sweden almost a half of respondents are not
religious (49.6%) and 31.9% are religious, 18.5% sweds are
atheists.The trends seem to be opposite.
Russia seem to be more religious country than Sweden.
It can be a relationship between religiosity and country.
————————————————————————-
[Ok bye…]