R #6

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

21 Terms

1
New cards

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

2
New cards

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)

3
New cards

How to renames columns with dplyr in R

df <- df|> rename(new_name1 = old_name1 , new_name2 = old_name2 )

4
New cards

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

5
New cards

How to modify existing column in dplyr in R

df <- df|> mutate(inflation = inflation / 100 )

6
New cards

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

7
New cards

How to separate column with tidyr in R and keeping it

df <- df|> separate( country_year, into=c(“country”, “year” ), sep=”_”, rempve=FALSE)

8
New cards

How to change type of character values into nummeric with dplyr in R

df <- df|> mutate(year = as.integer(year) )

9
New cards

How delete columns with dplyr in R

df<- df|> select(-c(column1, column2))

10
New cards

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

11
New cards

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

12
New cards

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))

)

13
New cards

What are the 4 joins command in dplyr in R

knowt flashcard image
14
New cards

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

15
New cards

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

16
New cards

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

17
New cards

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


18
New cards

How to find which country was duplicated in dplyr R

df |>

..filter(duplicated(country)) |>

..distinct(country)


#distinct returns every duplicate just once

19
New cards

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

…

20
New cards

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()

21
New cards

How to check whenever data frame is grouped in R ?

group_vars(df)


return:

0 .. no grouping

“country” .. grouped by country