iris[1:4]
과 iris[,1:4]
은 모두 열을 부분 집합하는 올바른 방법입니다. 예 :
> iris[1:4] %>% head()
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
> iris[,1:4] %>% head()
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
()
이 경우 아무 것도 수행하지 않습니다. 의 apply(.[(1:4)], 1, sd)
은 "파이프 앞의 출력을이 위치로 옮깁니다"라는 파이핑 구문입니다. 따라서이 경우 iris
은 mutate
(기본값)의 첫 번째 인수 인 과에서 .[(1:4)]
까지 파이프됩니다.이 값은 iris[(1:4)]
입니다. 다음은 동일한 출력을 제공합니다.
> iris %>% mutate(stDev = apply(.[1:4], 1, sd)) %>% head
Sepal.Length Sepal.Width Petal.Length Petal.Width Species stDev
1 5.1 3.5 1.4 0.2 setosa 2.179449
2 4.9 3.0 1.4 0.2 setosa 2.036950
3 4.7 3.2 1.3 0.2 setosa 1.997498
4 4.6 3.1 1.5 0.2 setosa 1.912241
5 5.0 3.6 1.4 0.2 setosa 2.156386
6 5.4 3.9 1.7 0.4 setosa 2.230844
> iris %>% mutate(stDev = apply(iris[1:4], 1, sd)) %>% head
Sepal.Length Sepal.Width Petal.Length Petal.Width Species stDev
1 5.1 3.5 1.4 0.2 setosa 2.179449
2 4.9 3.0 1.4 0.2 setosa 2.036950
3 4.7 3.2 1.3 0.2 setosa 1.997498
4 4.6 3.1 1.5 0.2 setosa 1.912241
5 5.0 3.6 1.4 0.2 setosa 2.156386
6 5.4 3.9 1.7 0.4 setosa 2.230844