R-Blogger๋ธ๋ก๊ทธยทํด์คํ๊ตญ์ด2024-12-22
Tidyverse ๐ช์์ Polars ๐ปโโ๏ธ๊น์ง: ๋ด ๋ ธํธ
Polars๋ฅผ ์ฌ์ฉํ R ๊ธฐ๋ฐ ํ์ด์ฌ ๋ฐ์ดํฐ ๋ถ์ ์ฐ์ต ๋๊ธฐ๋ถ์ฌ Polars ๊ตฌ๋ฌธ์ dplyr๊ณผ ๋งค์ฐ ์ ์ฌํฉ๋๋ค. ํจ์ ์ฒด์ด๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ฉด ์ต์ํจ์ ๋ ๋๋ ์ ์์์ต๋๋ค. ์ด์ ๋ฐฐ์ด ์ธ๋ถ ์ฌํญ์ ์ค์ ์์ ์ ์ฉํด ๋ณผ ์ฐจ๋ก์
๋๋ค. ํ์ด์ ๋น๋๋ค! ๐ ๋ชฉํ ๋ฐ์ดํฐํ๋ ์ ์์ฑ ํํฐ, ์ ํ, ์์ฝ, ์ ์ฒด ๋ณํ, ๋ฌธ์์ด ๊ฒฐํฉ, ์กฐ๊ฑด ๋ถ๊ธฐ, ์กฐ์ธ, ๋๋ฏธ/ํผ๋ฒ/์ธํผ๋ฒ ์ ์ฉํ ์๋ฃ ๋ฐ ํ์ต ๋ด์ฉ ์ ๋ฆฌ ๋ฐ์ดํฐํ๋ ์ ์์ฑ (tidyverse ์์) ๋ค์์ tidyverse๋ฅผ ์ฌ์ฉํ R ์์์
๋๋ค. ์๋ ์ฝ๋๋ฅผ ๋ณด๋ฉฐ Polars ๋ณํ์ ํ์ธํฉ๋๋ค. library(tidyverse) library(reticulate) use_virtualenv('path/to/your/environment') df %% filter(age = 30) %% select(1:3, starts_with("performance"), starts_with("salary")) %% summarize(across(.cols = where(is.numeric), .fns = mean, .names = "mean_{.col}")) Polars ๋ณํ ์์ Polars์์๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํฉ๋๋ค. df .filter(pl.col('age') = 30) .select(df.columns[0:4]+['^performance.*$','^salary.*$']) .select(pl.col(pl.Int64).mean().name.prefix('mean_')) ์ ๊ท์์์ ^์ $๋ฅผ ์ฌ์ฉํด ์ปฌ๋ผ๋ช
์ ์ ํํ ์ง์ ํด์ผ ํ๋ค๋ ์ ์ด ๋ค์ ์ด์ํฉ๋๋ค. ์ด ๋ณํ: ๋ฌธ์์ด ๊ฒฐํฉ (mutate, paste) ๋ชจ๋ ๋ฌธ์์ด ์ปฌ๋ผ์ ๊ณต๋ฐฑ์ผ๋ก ๊ฒฐํฉํ ์ ์ปฌ๋ผ์ ๋ง๋ค๊ณ ์ ํฉ๋๋ค. tidyverse: df %% rowwise() %% transmute(combination_of_character = paste(across(where(is.character)), collapse = " ")) %% select(combination_of_character) Polars: df .with_columns( pl.concat_str( pl.col(pl.String), separator=" " ).alias('combination_of_character') ) .select(pl.col('combination_of_character')) ์ด ๋ณํ: ๋ ๊ฐ ์ปฌ๋ผ ๊ฒฐํฉ (glue) ์ฐ๋ น๊ณผ ๊ธ์ฌ๋ฅผ ํ์ดํ(-)์ผ๋ก ์ฐ๊ฒฐํ ์ ์ปฌ๋ผ์ ๋ง๋ค๊ณ ์ ํฉ๋๋ค. tidyverse: df %% mutate(age_salary = paste0(age, "-", salary)) %% select(name, age_salary) Polars: df .with_columns( age_salary=pl.format('{}-{}',pl.col('age'),pl.col('salary')) ) .select(pl.col('name','age_salary')) ํ ๊ฐ ์ปฌ๋ผ๋ง ๋ง๋ค ๋๋ alias ์์ด format๋ง ์ฌ์ฉํด๋ ๋ฉ๋๋ค. ์ด ๋ณํ: ์ ๊ท์ ์ถ์ถ (area_code_and_salary) ์ฃผ์์์ ๋๋ก ๋ฒํธ๋ฅผ ์ถ์ถํด ๊ธ์ฌ์ ๊ฒฐํฉํ ์ ์ปฌ๋ผ์ ๋ง๋ค๊ณ ์ ํฉ๋๋ค. tidyverse: df %% mutate(area_code_and_salary = paste0(str_extract(address, "\\d{0,5}"), " ", salary)) %% select(area_code_and_salary) Polars: df .select( pl.concat_str( pl.col('address').str.extract(r'^(\d{0,5})'), pl.lit(" "), pl.col('salary') ).alias('area_code_and_salary') ) ์กฐ๊ฑด ๋ถ๊ธฐ (case_when) ์ฃผ์์ ๋ฐ๋ผ 'familiarity' ์ปฌ๋ผ์ ์์ฑํฉ๋๋ค. tidyverse: df %% mutate(familiarity = case_when( str_detect(address, "OH") ~ "local", str_detect(address, "NY") ~ "foodie", TRUE ~ "elsewhere" )) Polars: df .with_columns( pl.when(pl.col('address').str.contains('OH')).then(pl.lit('local')) .when(pl.col('address').str.contains('NY')).then(pl.lit('foodie')) .otherwise(pl.lit('elsewhere')) .alias('familiarity') ) ์ ๊ท์์ผ๋ก ์ด๋ฉ์ผ ์ ๋ถ ์ถ์ถ (lookโahead / lookโback ๋ถ๊ฐ) Polars์์๋ ์ ๊ท์์ lookโahead(.*(?=@))๋ lookโback์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฃนํ ๋ฐ ์ํํธ (Group_by, Shift, Forward_Fill) ๋ถ์๋ณ๋ก ๊ธ์ฌ๋ฅผ ํ ์ค์ฉ ์ํํธํ๊ณ , ๊ฒฐ์ธก๊ฐ์ ์ ๋ถ ์ฑ์ฐ๋ ์์์
๋๋ค. tidyverse: df %% group_by(department) %% summarize( name = name, salary_shift = case_when( n() == 1 ~ salary, TRUE ~ lead(salary) ) ) %% fill(salary_shift, .direction = "down") Polars (๋จ์ผ ๋ผ์ธ ์ํํธ์ ์ ๋ถ ์ฑ์ฐ๊ธฐ): df .group_by('department') .agg( pl.col('name'), pl.when(pl.col('salary').len()==1).then(pl.col('salary')) .otherwise(pl.col('salary').shift(-1)) .alias('salary_shift') ) .explode('name','salary_shift') .with_columns( pl.col('salary_shift').forward_fill() ) Polars๋ ๊ทธ๋ฃนํ ์ ๋ฆฌ์คํธ ์ปฌ๋ผ์ ์์ฑํ๋ฏ๋ก explode() ๋ก ํผ์น ๋ค forward_fill() ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๋๋ฏธ ๋ณํ ๋ฐ ํผ๋ฒ (to_dummies / pivot) ๋ถ์๋ณ ๋๋ฏธ ๋ณ์๋ฅผ ๋ง๋ค๊ณ , ํผ๋ฒ์ผ๋ก ์ํ๋ณ ๋ถํฌ๋ฅผ ํ์ธํฉ๋๋ค. tidyverse ์์: df %% select(name, department) %% pivot_wider( id_cols = "name", names_from = "department", values_from = "department", values_fill = 0, values_fn = length, names_prefix = "department_" ) Polars to_dummies: df .select(['name','department']) .to_dummies(columns = 'department') Polars ํผ๋ฒ ์์ (์ํ๋ณ ๋ถํฌ): df .select(['name','address']) .with_columns( state = pl.col('address').str.extract(r'([A-Z]{2})$
์๋ฌธ URL
์ ์ฒด ๊ธ์ ์๋ฌธ ํ์ด์ง์์ ์ด์ด์ ์ฝ์ ์ ์์ต๋๋ค.
- ์์ฑ์
- R-Blogger
- ์ถ์ฒ
- R-Blogger
- ํ๋ซํผ
- R-Blogger
- ๋ถ๋ฅ
- ๋ธ๋ก๊ทธยทํด์ค
- ์ธ์ด
- ํ๊ตญ์ด
- ๋ฐํ์ผ
- 2024-12-22