subset()
Descrition: Return subsets of vectors, matrices or data frames which meet conditions.
Usages
subset(dataframe, select, subset)
- dataframe : 조회할 데이터 세트
- select : 열이름
- subset : 조건식
ex1) iris 데이터의 Species 열에서 Petal.Length > 1.7 데이터를 조회하라
> subset(iris, select = Species, subset=(Petal.Length>1.7))
ex2) iris 데이터에서 Sepal.Width > 3 이고 Petal.Width == 0.2인 Sepal.Length, Petal.Length, Species 열을 조회하라
> subset(iris, select = c(Sepal.Length, Petal.Length, Species), subset=(c(Sepal.Width > 1.7 & Petal.Width == 0 ))
2015년 1월 10일 토요일
2014년 12월 23일 화요일
attach() 오브젝트이름을 기억시키는 함수
attach()
Description : The database is attached to the R search path. This means that the database is searched by R when evaluating a variable, so objects in the database can be accessed by simply giving their names.
attach(data셋)을 하면 data에 있는 오브젝트(열값, 속성)이름만으로 값 출력가능
usedcars 데이터의 model값을 접근할려면,
> usedcars$model
로 해야하지만,
> attach(usedcars)를 하면
usedcars에 있는 오브젝트 이름만으로 접근가능
> model
attach(data셋)을 하면 data에 있는 오브젝트(열값, 속성)이름만으로 값 출력가능
usedcars 데이터의 model값을 접근할려면,
> usedcars$model
로 해야하지만,
> attach(usedcars)를 하면
usedcars에 있는 오브젝트 이름만으로 접근가능
> model
> head(usedcars,10) year model price mileage color transmission 1 2011 SEL 21992 7413 Yellow AUTO 2 2011 SEL 20995 10926 Gray AUTO 3 2011 SEL 19995 7351 Silver AUTO 4 2011 SEL 17809 11613 Gray AUTO 5 2012 SE 17500 8367 White AUTO 6 2010 SEL 17495 25125 Silver AUTO 7 2011 SEL 17000 27393 Blue AUTO 8 2010 SEL 16995 21026 Silver AUTO 9 2011 SES 16995 32655 Silver AUTO 10 2010 SES 16995 36116 Silver AUTO > head(usedcars$model, 10) [1] "SEL" "SEL" "SEL" "SEL" "SE" "SEL" "SEL" "SEL" "SES" "SES" > attach(usedcars) > head(model, 10) [1] "SEL" "SEL" "SEL" "SEL" "SE" "SEL" "SEL" "SEL" "SES" "SES"
head(usedcars$model,10) 과 head(model,10)은 출력값은 동일
2014년 11월 24일 월요일
R round() 반올림 함수
round(x, digits = 0)
x : a numeric vector(실수값)
digits : integer indicating the number of decimal places(round), 반올림된 자리수
* digits = 1 : 소수점 아래 자리
* digits = -1 : 소수점 윗 자리
* digits = 0 : 소수점(원점) 자리
x : a numeric vector(실수값)
digits : integer indicating the number of decimal places(round), 반올림된 자리수
* digits = 1 : 소수점 아래 자리
* digits = -1 : 소수점 윗 자리
* digits = 0 : 소수점(원점) 자리
> round(123.456, digits = 1) # 소숫점 아래 첫째자리 [123.5] > round(123.456, digits = 0) # 소숫점(원점) 자리 [1] 123 > round(123.456, digits= -1) # 소숫점 윗 첫째자리 [1] 120ex) 100 - 900(단위:100)사이 랜덤 숫자 5개 출력
> round(runif(5, min=100,max=900), digits=-2) # digits = -2 : 둘째 자리까지 표현 [1] 700 500 700 400 300
라벨:
R 문법
,
R programming
2014년 11월 22일 토요일
R 데이터 유형과 객처
벡터 : 벡터는 스칼라의 조합
I. R 데이터 타입(data type)
II. 데이터 오브젝트(data object)
I. R 데이터 타입(data type)
| 종류 | 의미 |
|---|---|
| NULL | 비었다는 의미. 0과는 다르다. |
| NA | 부정 데이터. 결측값(Not available) |
| NaN | 비수치(Not a Number). 0/0 등을 계산하면 NaN이 된다. |
| Inf / -Inf | 무한대. 1/0 등을 계산하면 Inf가 된다.,Inf(+무한대), -Inf (-무한대) |
| 실수(numeric) | 숫자 형태의 데이터. 정수인 ingeger 와 소수점이 있는 double이 있다 |
| 복소수(complex) | 1+2i 등 |
| 문자열(character) | 문자형 데이터. 인용부호 “ ”로 에워싼 문자열 |
| 인자(factor) | 1:남성, 2:여성 등의 수준을 나타낸다. |
| 논리값(logical) | TRUE / FALSE |
| 날짜값(date) | “2014-05-23″등의 날짜나 시각 |
II. 데이터 오브젝트(data object)
| 종류 | 의미 |
|---|---|
| Vector, Scalar | 1차원 가진 벡터 |
| Matrix | 2차원 가진 벡터. matrix(이름, 행수, 열수) |
| Arrary | 다차원 가지 벡터. |
| Factor | 무한대. 1/0 등을 계산하면 Inf가 된다.,Inf(+무한대), -Inf (-무한대) |
| List | 서로다른 유형의 데이터로 구성된 데이터 객체. list( , , , ...) |
| Data Frame | 행렬과 유사한 행태나 열의 자료형이 서로 다를수 있음. 리스트의 한 형태. dafa.frame( , , , ...) |
2014년 11월 20일 목요일
grep()
grep(pattern,x)
벡터 x 에서 특정패턴을 찾아서 그 위치를 출력
x = c("apple","Apple","Aple","banana","grape","aaple","apple")
grep("apple", x)
벡터 x 에서 특정패턴을 찾아서 그 위치를 출력
x = c("apple","Apple","Aple","banana","grape","aaple","apple")
grep("apple", x)
[1] 1 7
grep()함수는 기본적으로 특정 단어가 나오는 위치를 알려주지만 value 라는 파라미터를 이용하면 값 자체를 출력
grep("apple",x, value=T)
[1] "apple" "apple"
예제)
> x = c("apple1","apple2","apple3","apple4","Apple","Apleap","apleap1", "banana","grape","aaple","apple")
> # case 1 : apple 단어 들어간 위치 출력
> grep("apple", x)
[1] 1 2 3 4 11
> # case 2 : apple 단어가 들어간 값 출력
> grep("apple",x, value=T)
[1] "apple1" "apple2" "apple3" "apple4" "apple"
> # case 3 : ^ (carrot) 첫글자가 소문자 a 로 시작하는 값 출력
> grep("^ap+",x, value=T)
[1] "apple1" "apple2" "apple3" "apple4" "apleap1" "apple"
> # case 4 : ^ (carrot) 첫글자가 대문자 a 로 시작하는 값 출력
> grep("^Ap+",x, value=T)
[1] "Apple" "Apleap"
> # case 5 : ap 단어가 들어간 값 출력
> grep("ap+",x, value=T)
[1] "apple1" "apple2" "apple3" "apple4" "Apleap" "apleap1" "grape" "aaple" "apple"
> # case 6 : 마지막 끝나는 문자가 ap 인 값 출력
> grep("ap$",x, value=T)
[1] "Apleap"
> # case 7 : 숫자 1 ~ 3 을 포함하는 값 출력
> grep("[1:3]",x, value=T)
[1] "apple1" "apple3" "apleap1"
> # case 8 : 숫자가 포함하는 모든 값 출력
> grep("[[:digit:]]",x, value=T)
[1] "apple1" "apple2" "apple3" "apple4" "apleap1"
> # case 9 : 소문자를 포함한 값 모두 출력
> grep("[[:lower:]]",x, value=T)
[1] "apple1" "apple2" "apple3" "apple4" "Apple" "Apleap" "apleap1" "banana" "grape"
[10] "aaple" "apple"
> # case 10 : 대문자를 포함한 값 모두 출력
> grep("[[:upper:]]",x, value=T)
[1] "Apple" "Apleap"
피드 구독하기:
글
(
Atom
)