dplyr의 mutate_each / summarise_each : 특정 열을 선택하고 변경된 열에 새 이름을 지정하는 방법은 무엇입니까?
나는 dplyr
동사 에 대해 약간 혼란스러워mutate_each.
mutate
데이터 열을 예를 들어 z 점수로 변환하고 data.frame에 새 열을 만드는 데 기본을 사용하는 것은 매우 간단합니다 (여기서는 이름 사용 z_score_data
).
newDF <- DF %>%
select(one_column) %>%
mutate(z_score_data = one_column - (mean(one_column) / sd(one_column))
그러나 변환하고 싶은 데이터 열이 많기 때문에 mutate_each
동사를 사용해야 할 것 같습니다 .
newDF <- DF %>%
mutate_each(funs(scale))
여태까지는 그런대로 잘됐다. 그러나 아직까지 나는 알아낼 수 없었습니다.
- 이 새 열에 적절한 이름을 지정하려면
mutate
어떻게해야합니까? select
첫 번째 경우에서 와 같이 변경하려는 특정 열을 어떻게 선택할 수 있습니까?
당신의 도움을 주셔서 감사합니다.
dplyr> = 0.4.3.9000 업데이트
dplyr 개발 버전 0.4.3.9000에서, 내부 이름 (글을 쓰는 시점에서) mutate_each
와 summarise_each
에서 언급 한 바와 같이 간단 해졌습니다 뉴스 :
함수와 변수 이름을 모두 강제로 포함 할 수 있도록
summarise_each()
및 의 이름 지정 동작mutate_each()
이 조정되었습니다.summarise_each(mtcars, funs(mean = mean), everything())
mutate_each
/ 내부에 하나의 함수 만 적용 summarise_each
하고 해당 열에 새 이름을 지정 하려는 경우 주로 중요 합니다.
차이점을 보여주기 위해 아래 옵션 a.2 와 달리 새로운 이름 지정 기능을 사용한 dplyr 0.4.3.9000의 출력은 다음과 같습니다.
library(dplyr) # >= 0.4.3.9000
iris %>% mutate_each(funs(mysum = sum(.)), -Species) %>% head()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_mysum Sepal.Width_mysum
#1 5.1 3.5 1.4 0.2 setosa 876.5 458.6
#2 4.9 3.0 1.4 0.2 setosa 876.5 458.6
#3 4.7 3.2 1.3 0.2 setosa 876.5 458.6
#4 4.6 3.1 1.5 0.2 setosa 876.5 458.6
#5 5.0 3.6 1.4 0.2 setosa 876.5 458.6
#6 5.4 3.9 1.7 0.4 setosa 876.5 458.6
# Petal.Length_mysum Petal.Width_mysum
#1 563.7 179.9
#2 563.7 179.9
#3 563.7 179.9
#4 563.7 179.9
#5 563.7 179.9
#6 563.7 179.9
새 이름을 제공하지 않고 1 개의 함수 만 제공하면 dplyr은 기존 열을 변경합니다 (이전 버전에서와 같이).
iris %>% mutate_each(funs(sum), -Species) %>% head()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 876.5 458.6 563.7 179.9 setosa
#2 876.5 458.6 563.7 179.9 setosa
#3 876.5 458.6 563.7 179.9 setosa
#4 876.5 458.6 563.7 179.9 setosa
#5 876.5 458.6 563.7 179.9 setosa
#6 876.5 458.6 563.7 179.9 setosa
이 새로운 기능은 다음 릴리스 버전 0.4.4에서 CRAN을 통해 사용할 수 있다고 가정합니다.
dplyr 버전 <= 0.4.3 :
mutate 에서처럼 이러한 새 열에 적절한 이름을 지정하려면 어떻게해야합니까?
a) mutate_each
/에 적용되는 1 개의 기능summarise_each
mutate_each
또는 내부에 하나의 함수 만 적용 하면 /에 명명 된 벡터를 제공하지 않는 한summarise_each
기존 열이 변환되고 이름이 예전 그대로 유지됩니다 (옵션 a.4 참조).mutate_each_
summarise_each_
여기 예시들이 있습니다 :
a.1 단 하나의 기능-> 기존 이름 유지
iris %>% mutate_each(funs(sum), -Species) %>% head()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 876 459 564 180 setosa
#2 876 459 564 180 setosa
#3 876 459 564 180 setosa
#4 876 459 564 180 setosa
#5 876 459 564 180 setosa
#6 876 459 564 180 setosa
a.2 또한 새 열 확장명을 지정하는 경우 :
iris %>% mutate_each(funs(mysum = sum(.)), -Species) %>% head()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 876 459 564 180 setosa
#2 876 459 564 180 setosa
#3 876 459 564 180 setosa
#4 876 459 564 180 setosa
#5 876 459 564 180 setosa
#6 876 459 564 180 setosa
a.3 열마다 새 이름을 수동으로 지정합니다 (하지만 몇 개의 열에 만 실용적 임).
iris %>% mutate_each(funs(sum), SLsum = Sepal.Length,SWsum = Sepal.Width, -Species) %>% head()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species SLsum SWsum
#1 5.1 3.5 1.4 0.2 setosa 876 459
#2 4.9 3.0 1.4 0.2 setosa 876 459
#3 4.7 3.2 1.3 0.2 setosa 876 459
#4 4.6 3.1 1.5 0.2 setosa 876 459
#5 5.0 3.6 1.4 0.2 setosa 876 459
#6 5.4 3.9 1.7 0.4 setosa 876 459
a.4 명명 된 벡터를 사용하여 새 이름으로 추가 열을 만듭니다.
사례 1 : 원래 열 유지
옵션 a.1, a.2 및 a.3과 달리 dplyr은 기존 열을 변경하지 않고이 접근 방식에서 새 열을 만듭니다. 새 열의 이름은 미리 만든 명명 된 벡터의 이름과 같습니다 ( vars
이 경우).
vars <- names(iris)[1:2] # choose which columns should be mutated
vars <- setNames(vars, paste0(vars, "_sum")) # create new column names
iris %>% mutate_each_(funs(sum), vars) %>% head
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sum Sepal.Width_sum
#1 5.1 3.5 1.4 0.2 setosa 876.5 458.6
#2 4.9 3.0 1.4 0.2 setosa 876.5 458.6
#3 4.7 3.2 1.3 0.2 setosa 876.5 458.6
#4 4.6 3.1 1.5 0.2 setosa 876.5 458.6
#5 5.0 3.6 1.4 0.2 setosa 876.5 458.6
#6 5.4 3.9 1.7 0.4 setosa 876.5 458.6
사례 2 : 원래 열 제거
보시다시피이 접근 방식은 기존 열을 변경하지 않고 지정된 이름으로 새 열을 추가합니다. 원래 열을 유지하지 않고 새로 만든 열 (및 다른 열) 만 유지하려는 경우 select
나중에 문을 추가 할 수 있습니다 .
iris %>% mutate_each_(funs(sum), vars) %>% select(-one_of(vars)) %>% head
# Petal.Length Petal.Width Species Sepal.Length_sum Sepal.Width_sum
#1 1.4 0.2 setosa 876.5 458.6
#2 1.4 0.2 setosa 876.5 458.6
#3 1.3 0.2 setosa 876.5 458.6
#4 1.5 0.2 setosa 876.5 458.6
#5 1.4 0.2 setosa 876.5 458.6
#6 1.7 0.4 setosa 876.5 458.6
b) mutate_each
/에 적용되는 하나 이상의 기능summarise_each
b.1 dplyr이 새로운 이름을 알아 내도록합니다.
둘 이상의 함수를 적용한 경우 dplyr이 자체적으로 이름을 알아 내도록 할 수 있습니다 (기존 열을 유지함).
iris %>% mutate_each(funs(sum, mean), -Species) %>% head()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sum Sepal.Width_sum Petal.Length_sum
#1 5.1 3.5 1.4 0.2 setosa 876 459 564
#2 4.9 3.0 1.4 0.2 setosa 876 459 564
#3 4.7 3.2 1.3 0.2 setosa 876 459 564
#4 4.6 3.1 1.5 0.2 setosa 876 459 564
#5 5.0 3.6 1.4 0.2 setosa 876 459 564
#6 5.4 3.9 1.7 0.4 setosa 876 459 564
# Petal.Width_sum Sepal.Length_mean Sepal.Width_mean Petal.Length_mean Petal.Width_mean
#1 180 5.84 3.06 3.76 1.2
#2 180 5.84 3.06 3.76 1.2
#3 180 5.84 3.06 3.76 1.2
#4 180 5.84 3.06 3.76 1.2
#5 180 5.84 3.06 3.76 1.2
#6 180 5.84 3.06 3.76 1.2
b.2 수동으로 새 열 이름 지정
Another option, when using more than 1 function, is to specify the column name extension on your own:
iris %>% mutate_each(funs(MySum = sum(.), MyMean = mean(.)), -Species) %>% head()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_MySum Sepal.Width_MySum Petal.Length_MySum
#1 5.1 3.5 1.4 0.2 setosa 876 459 564
#2 4.9 3.0 1.4 0.2 setosa 876 459 564
#3 4.7 3.2 1.3 0.2 setosa 876 459 564
#4 4.6 3.1 1.5 0.2 setosa 876 459 564
#5 5.0 3.6 1.4 0.2 setosa 876 459 564
#6 5.4 3.9 1.7 0.4 setosa 876 459 564
# Petal.Width_MySum Sepal.Length_MyMean Sepal.Width_MyMean Petal.Length_MyMean Petal.Width_MyMean
#1 180 5.84 3.06 3.76 1.2
#2 180 5.84 3.06 3.76 1.2
#3 180 5.84 3.06 3.76 1.2
#4 180 5.84 3.06 3.76 1.2
#5 180 5.84 3.06 3.76 1.2
#6 180 5.84 3.06 3.76 1.2
How can I select certain columns that I wish to mutate, like I did with select in the first case?
You can do that by referencing the columns to be mutated (or left out) by giving their names like here (mutate Sepal.Length, but not Species):
iris %>% mutate_each(funs(sum), Sepal.Length, -Species) %>% head()
In addition, you can use special functions to select columns to be mutated, all columns that start with or contain a certain word etc by using for example:
iris %>% mutate_each(funs(sum), contains("Sepal"), -Species) %>% head()
For more information of those functions, see ?mutate_each
and ?select
.
Edit 1 after comment:
If you want to use standard evaluation, dplyr supplies SE-versions of most functions ending with an addtional "_". So in this case you would use:
x <- c("Sepal.Width", "Sepal.Length") # vector of column names
iris %>% mutate_each_(funs(sum), x) %>% head()
Notice the mutate_each_
I used here.
Edit 2: updated with option a.4
mutate_each
will be deprecated, consider using mutate_at
. From dplyr_0.5.0
documentation:
In the future mutate_each() and summarise_each() will be deprecated in favour of a more featureful family of functions: mutate_all(), mutate_at(), mutate_if(), summarise_all(), summarise_at() and summarise_if().
Apply a function to all variables except Species
:
Warning: '.cols' param is deprecated, see note at the bottom!
iris %>% mutate_at(.cols=vars(-Species), .funs=funs(mysum = sum(.))) %>% head()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_mysum Sepal.Width_mysum
1 5.1 3.5 1.4 0.2 setosa 876.5 458.6
2 4.9 3.0 1.4 0.2 setosa 876.5 458.6
3 4.7 3.2 1.3 0.2 setosa 876.5 458.6
4 4.6 3.1 1.5 0.2 setosa 876.5 458.6
5 5.0 3.6 1.4 0.2 setosa 876.5 458.6
6 5.4 3.9 1.7 0.4 setosa 876.5 458.6
Petal.Length_mysum Petal.Width_mysum
1 563.7 179.9
2 563.7 179.9
3 563.7 179.9
4 563.7 179.9
5 563.7 179.9
6 563.7 179.9
Apply a function to a subset of variables
vars_to_process=c("Petal.Length","Petal.Width")
iris %>% mutate_at(.cols=vars_to_process, .funs=funs(mysum = sum(.))) %>% head()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Petal.Length_mysum Petal.Width_mysum
1 5.1 3.5 1.4 0.2 setosa 563.7 179.9
2 4.9 3.0 1.4 0.2 setosa 563.7 179.9
3 4.7 3.2 1.3 0.2 setosa 563.7 179.9
4 4.6 3.1 1.5 0.2 setosa 563.7 179.9
5 5.0 3.6 1.4 0.2 setosa 563.7 179.9
6 5.4 3.9 1.7 0.4 setosa 563.7 179.9
Update! for dplyr 0.7.1 version (2017-08-08)
If you see the message:
.cols
has been renamed and is deprecated, please use.vars
then change .cols
by .vars
.
iris %>% mutate_at(.vars=vars(-Species), .funs=funs(mysum = sum(.))) %>% head()
Another example:
iris %>% mutate_at(.vars=vars(Sepal.Width), .funs=funs(mysum = sum(.))) %>% head()
Is equivalent to:
iris %>% mutate_at(.vars=vars("Sepal.Width"), .funs=funs(mysum = sum(.))) %>% head()
Also, in this version the mutate_each
is deprecated:
mutate_each()
is deprecated. Usemutate_all()
,mutate_at()
ormutate_if()
instead. To mapfuns
over a selection of variables, usemutate_at()
'IT TIP' 카테고리의 다른 글
NodeJS는 정말 단일 스레드입니까? (0) | 2020.12.07 |
---|---|
"ui-sref"를 조건부로 실행하는 방법은 무엇입니까? (0) | 2020.12.07 |
if 문 다음에 나오는 변수 선언 (0) | 2020.12.07 |
페이지의 특정 부분 만 표시하는 iframe (0) | 2020.12.07 |
문자열 매개 변수가있는 C # TrimStart (0) | 2020.12.07 |