Dplyr: `starts_with`,`ends_with`,`contains` 및`matches`둜 λ³€μˆ˜λ₯Ό μ„ νƒν•˜λ©΄ 주어진 νŒ¨ν„΄μ΄ 없을 λ•Œ 잘λͺ»λœ κ²°κ³Όκ°€ λ°˜ν™˜λ©λ‹ˆλ‹€.

에 λ§Œλ“  2014λ…„ 07μ›” 15일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: tidyverse/dplyr

λ™μž‘μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

> d <- tbl_df(data.frame(xxx = 1:2, yyy = 1:2, bxx = 1:2, bbb = 1:2))
> d %>% select(starts_with('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2
> d %>% select(ends_with('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2
> d %>% select(matches('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2
> d %>% select(contains('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2

λΆ„λͺ…νžˆ select ν•¨μˆ˜λŠ” 데이터 ν”„λ ˆμž„μ˜ λͺ¨λ“  열을 λ°˜ν™˜ν•˜μ§€ μ•Šμ•„μ•Όν•©λ‹ˆλ‹€. μœ μš©ν•œ λ©”μ‹œμ§€μ™€ ν•¨κ»˜ 였λ₯˜λ₯Ό λ°œμƒ μ‹œν‚€κ±°λ‚˜ 빈 데이터 ν”„λ ˆμž„μ„ λ°˜ν™˜ν•΄μ•Όν•©λ‹ˆλ‹€. μ–΄λŠ 것이 더 λ‚˜μ€μ§€ 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

λ‚΄κ°€ λ³Ό 수 μžˆλ“―μ΄ λ¬Έμ œλŠ” select_vars_q ν•¨μˆ˜μ˜ select_funs λΌλŠ” ν•¨μˆ˜ λͺ©λ‘μ— μžˆμŠ΅λ‹ˆλ‹€. κ±°κΈ°μ—μ„œ 였λ₯˜λ₯Ό ν¬μ°©ν•˜κ³  그에 따라 무엇을 λ°˜ν™˜ν• μ§€ κ²°μ •ν•΄μ•Όν•©λ‹ˆλ‹€. ν’€ λ¦¬ν€˜μŠ€νŠΈλ₯Ό μ œμΆœν•˜κ²Œλ˜μ–΄ κΈ°μ˜μ§€λ§Œ κ°€μž₯ μ μ ˆν•œ λ°˜ν™˜ 값이 무엇이라고 μƒκ°ν•˜λŠ”μ§€ 듣지 μ•Šκ³  μž‘μ—…μ„ν•˜κ³  μ‹Άμ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€. :)

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

이것이 κ΄€λ ¨ λ¬Έμ œμΈμ§€ ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ λ‹€μŒ λ‚΄μš©μ΄ μΌμΉ˜ν•˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

> data_frame(a=1, ba=1) %>%  select(starts_with("a"), ends_with("b")) %>% names
character(0)
> 
> data_frame(a=1, ab=1) %>%  select(starts_with("a"), ends_with("b")) %>% names
[1] "a"  "ab"

λͺ¨λ“  3 λŒ“κΈ€

"Failed to select any columns"와 같은 였λ₯˜κ°€ λ°œμƒν•΄μ•Όν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. λ˜ν•œ select(mtcars, -(mpg:carb)) 와 같은 μΌ€μ΄μŠ€λ₯Ό μ²˜λ¦¬ν•΄μ•Όν•©λ‹ˆλ‹€.

λ©‹μžˆλŠ”. 일주일 정도면 ν’€ λ¦¬ν€˜μŠ€νŠΈλ₯Ό λ³΄λ‚΄κ² μŠ΅λ‹ˆλ‹€. 휴일 ATM.

이것이 κ΄€λ ¨ λ¬Έμ œμΈμ§€ ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ λ‹€μŒ λ‚΄μš©μ΄ μΌμΉ˜ν•˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

> data_frame(a=1, ba=1) %>%  select(starts_with("a"), ends_with("b")) %>% names
character(0)
> 
> data_frame(a=1, ab=1) %>%  select(starts_with("a"), ends_with("b")) %>% names
[1] "a"  "ab"
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰