DANE:
The basic principles are: be consistent, write dates like YYYY-MM-DD, do not
leave any cells empty, put just one thing in a cell, organize the data as a
single rectangle (with subjects as rows and variables as columns, and with a
single header row), create a data dictionary, do not include calculations in
the raw data files, do not use font color or highlighting as data, choose
good names for things, make backups, use data validation to avoid data entry
errors, and save the data in plain text files
#############################################################################
##
#############################################################################
##
[Link](vector) - usuwa NA
colnames(df) - wszystkie nazwy zmiennych
summary(df) - dla liczbowych zmiennych średnia, mediana, NA, etc
######################
ggplot(data frame)+
aes(x =, y=, color=/fill=[gdyBoxplot])+
geom_point()+
labs(title="", x="", y="")+
theme_bw()+
theme([Link].x = element_text(angle = 90, [hitTAB-różne arg])
theme_minimal()+
ylim(c(0,20))+ #zakres osi y
facet_wrap(vars(...)) #musi byc vars()
ggsave("[Link]/.pdf", width=[inch] ,height=[inch] ,dpi= 300)
table([character_vector]) # wyswietla liczebnosci kazdego characteru
factor_vector <- factor(character_vector, levels = c("dog","cat","mouse")) #
kolejnosc wazna [avoid casting numbers as factors]
?-fct_remap (sortuje factor na podst num values)-
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
#############################################
arrange(desc()), filter([var]%in% [c(values)]), select, group_by/summarize,
mutate, %>%, & | != -[exclude var]
mutate([[Link]]=factor([[Link]], levels=c("..",".."))
tworzenie przedziałow kategorii#########################################
mutate([Link] = case_when([[Link]] <= 5 ~ '0-5', [[Link]] # 5 ~ '6+')
#zmienia na character, na factor trzeba dod zmieniac
?mutate #rozne rodzaje
group_by([var1],[var2],..)
summarize([[Link]]=mean(),... median(), sd(), min(), max(), first(),
last(), nth(), n(), n_distinct())
signif(x,digits=...) #all digits,
round(x,digits=...) #decimals
glimpse()
write_excel_csv(x=processed data, path="data/processed_data.csv")
library(janitor)
janitor::tabyl([var],..[var2]) #namespace , tabyl robi crosstables tez,
zmienne OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
janitor::clean_names()
#############################################################################
################.
factor() -moze zlimitowac dop wartosci - czyli tworzyc zmienna filtrujaca
library(forcats)
fct_rev() - factor reverse
fct_reorder() - order labels of a factor by another variable
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
#############################################################################
#######
bind_rows() #wiazanie zestawow danych ze soba, te same nazwy kolumn, porzadek
niewazny
clean_names()
select(var1=var.1a, var2,..) #w select mozna zmienic nazwy
///////////////////joining tables in dplyr
inner join # tylko wiersze ktore pasuja (te same zmienne kluczowe "key")
inner_join(tableA,tableB, by= c(key_columnA = key_columnB) # =? czy ,? i
moze trzeba "" po by=
left_join() - pierwsza tabela ma wszystkie wiersze, druga tylko pasujace
right_join() - odwrotnie
full_join() - wszystkie wiersze obu tabel
/////////////////////////////////////// long and wide format
pivot_longer(cols = c([kolumny do zestawienia w 1]), names_to= [nazwa kol z
nazwami starych kol], values_to= [nazwa kol z wartosciami])
pivot_wider()
starts_with("") - pozwala wybrac zmienne zaczynajace sie od danych slow
OOOOOOOOOOOOOOOOOOOOOOOOOOOOO
geom_tile() #heatmap?
#############################################################################
######
tidymodels
library(broom)
tidy() - porzadkuje wyniki modeli stat (testy t, regresja)
glance() - dod dane, np R2, AIC/BIC OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
########missing values - need to remove?!?
df <- df %>% tidyr::drop_na()
[Link]([Link] ~ [Link], paired=T, data= d.f.) %>% tidy() #zmienne w
formacie long!
[Link](df$zm.gr1, df$zm.gr2, paired=T) # dla zmiennych w formacie wide!
##################
library(GGally) #ma funkcje do par korelacji (wide data)
GGally::ggpairs(df, aes(color=[[Link]]))
###########GGPLOT opcje
...+
stat_summary(fun.y="mean",geom="point", pch=3,color="red") #boxploty pokazuja
mediany, to dodaje srednią-kropke
...+
geom_jitter(color= "grey") #to see actual values on a boxplot (x-axis has no
meaning) - visible overlapping points
...+
geom_smooth(method= "lm", se= FALSE) #dodanie lini trendu na scatterplocie
stat_smooth() #takie samo j.w.???
lm(y ~ x1 + x2, df) %>% tidy() #model lm chyba sam robi dummy
coding (0,1)
###############ANOVA
aov([Link] ~ [Link], data = [Link]) %>% tidy()
#post-hoc
[Link]([Link], [Link], [Link] = "bonferroni") %#% tidy() #
...
#############################################################################
####
#############################################################################
#####
R programming 101 ################.......
######################################
mutate(size = height > 1 & weight > 75, size = if_else(size == TRUE, "big",
"small"))
#####################################################
#################################################3
############################################ gather
dem_score %>% gather(key=year, value=score, -country)
The first argument, key, is the name of our ‘gathered’ variable. We’re
smushing all of the columns that have year names and calling the column
names, or the key the name of year. The second argument, value, is the actual
observations. Finally, if we don’t want a column to be gathered, we can leave
it out with the - notation (here, -country).
######################################### spread
spread() does the opposite of gather(). It “unbundles” a column into multiple
columns. This situation can happen because related measurements that consist
of an observation are collected separately, or someone has gathered the data
a little too enthusiastically.
gatheredData %>% spread(key=country, value=score)
########################################separate
"value1/value2/value3"
separate() is made to make this one variable into many other variable.
Separate takes the following arguments:
separate(col=HealthCodeEncounterCode, into=c("HealthCode", "EncounterCode"),
sep="/")
########################################3
broom - tidy()-wart regr glance()-statystyki podsum augment()-wart przewid y
#########################
Understand how tidyselect helper functions (contains(), all_of()) work with
select().
Understand how where() works with select and .is* functions
Learn how to combine helpers with c() and find the complement using -
Apply across() to work with mutate() to mutate multiple columns
Apply across() with summarize() to summarize multiple columns
#####################################
#################################
usuwanie tekstu
pattern <- "_pro"
my_df$X <- str_remove_all(my_df$X,pattern)