1/20
Looks like no tags are added yet.
How to sort data frames by values of one column (2)
df |> arrange(inflation) » sort from smallest to largest value in inflation
df|> arrange(desc(inflation)) » sort from largest to smallest
How to sort by values of two columns (2)
df |> arrange(year, inflation) » first sort by year and then inflation within this year
df|> arrange(desc(year), desc(inflation)
How to renames columns with dplyr in R
df <- df|> rename(new_name1 = old_name1 , new_name2 = old_name2 )
How to create a columns with dplyr in R
df <- df |> mutate( high_inflation = inflation >5 )
.. creates columns with TRUE if inflation over 5 and with FALSE otherwise
How to modify existing column in dplyr in R
df <- df|> mutate(inflation = inflation / 100 )
How to use relocate with dplyr in R (4)
relocate(inflation) » move inflation into the beggining
relocate(inflation ,.after=last_col()) » move inflation into the end
relocate(c(year,inflation) , .before=1) » moves year and inflation into the beggining
recolate( year:inflation,.after=3) » move everything between year and inflation after third column
How to separate column with tidyr in R and keeping it
df <- df|> separate( country_year, into=c(“country”, “year” ), sep=”_”, rempve=FALSE)
How to change type of character values into nummeric with dplyr in R
df <- df|> mutate(year = as.integer(year) )
How delete columns with dplyr in R
df<- df|> select(-c(column1, column2))
How to select all NA or only non-NA values in certains columns in dplyr in R
df |> filter(is.na(happiness)) .. select all NAs
df|> filter(! is.na(happiness)) .. select all Non-NAs
How to remove rows with missing values in dplyr R (2)
df |> drop_na() .. any row containing at least one NA
df|> drop_na(happiness) .. rows containing NA in happiness column
How to replace all NA values with average from several columns in dplyr R
df <- df|>
mutate(
happiness = replace_na(happiness, mean(happiness, na.rm=TRUE)),
inflation = replace_na(inflation ,mean(inflation, na.rm=TRUE))
)
What are the 4 joins command in dplyr in R

How to left_join works in dplyr in R
it uses all obsrvation from left side and add columns from right, puts NA if missing
What does semi join in dplyr R do (2)
left_df |> semi_join(right_df, by=”country”)
keep only rows appreating in both data sets and preserves only columns from the left data frame
What does anti join in dplyr R do
left_df |> anti_join(right_df , by = “country” )
keep observation from left_df that arent contained in right_df and keeps only columns from left_df
How to check duplicates in df in dplyr R
df |> country(country, year) |> filter(n > 1)
#filter all countries that appear more than once a year
How to find which country was duplicated in dplyr R
df |>
..filter(duplicated(country)) |>
..distinct(country)
#distinct returns every duplicate just once
How to calculate the average happiness of country from multiple year observation in dplyr R
df |> group_by(country) |> summarize(happy_mean = mean(happiness)
print »
Austria 7.1
Germany 6.54
…
What is important to remember after grouping countries in data frame in dplyr R
to remove the grouping for further work
df <- grouped_df |> ungroup()
How to check whenever data frame is grouped in R ?
group_vars(df)
return:
0 .. no grouping
“country” .. grouped by country