2019년 6월 20일 목요일

R: Data frames

1. 데이터 프레임에서 subset() 함수를 사용하여 조건을 만족하는 행만 추출하기

1
2
3
4
5
6
7
8
> # planets_df is pre-loaded in your workspace
> 
> # Select planets with diameter < 1
> subset(planets_df, subset = diameter < 1)
     name               type diameter rotation rings
1 Mercury Terrestrial planet    0.382    58.64 FALSE
2   Venus Terrestrial planet    0.949  -243.02 FALSE
4    Mars Terrestrial planet    0.532     1.03 FALSE
cs

"subset = " 뒤에 조건을 작성하면 된다.

2. order()함수를 이용하여 특정 열의 순위를 기준으로 데이터 프레임 정렬하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> # planets_df is pre-loaded in your workspace
> 
> # Use order() to create positions
> positions <-  order(planets_df$diameter)
> 
> positions
[11 4 2 3 8 7 6 5
> 
> # Use positions to sort planets_df
> planets_df[positions, ]
     name               type diameter rotation rings
1 Mercury Terrestrial planet    0.382    58.64 FALSE
4    Mars Terrestrial planet    0.532     1.03 FALSE
2   Venus Terrestrial planet    0.949  -243.02 FALSE
3   Earth Terrestrial planet    1.000     1.00 FALSE
8 Neptune          Gas giant    3.883     0.67  TRUE
7  Uranus          Gas giant    4.007    -0.72  TRUE
6  Saturn          Gas giant    9.449     0.43  TRUE
5 Jupiter          Gas giant   11.209     0.41  TRUE
cs


댓글 없음:

댓글 쓰기