x<-runif(10,0,1)
quantile(x,c(0.1,0.9))
mean(x,trim=0.1) #절사평균
?mean
---------------------------
#ppt 5.1 Introduction
meank<-function(x,k=0.1){
xt<-quantile(x,c(k,1-k))
mean(x[x>xt[1]&x<xt[2]]) #x>xt[1]과x<xt[2] 사이에 있는 x추출 ; &는 and
}
test<-rnorm(100)
meank(test)
#절사평균 구하는 함수 >> 절사평균은 이상치가 있을때 더 유의함~?
-------------------------------
#ppt 5.2 Arguments and veriables
plotsin<-function(xup=2*pi,...){
x<-seq(0,xup,l=100)
plot(x,sin(x),type="l",...)
}
plotsin(col='red')
?plot #plot 도움말, 꼼곰히 읽기
plotsin<-function(xup=2*pi,...){
x<-seq(0,xup,o=100)
plot(x,sin(x),type="o",...)
}
plotsin(col='blue')
-------------------------------
#5.2.3
functionx<-function(){x<-3}
functionx
test<-function(n,fun){
u<-runif(n)
fun(u)
}
test(10,sin)
------------------------
#5.2.4
#5.2.5 함수안에 또 함수
#5.3조건문
x<-runif(10)
if(x[1]>0.5){x[1]<-0.5} #if괄호안에 logical value작성
x<-runif(10)
if(min(x)>0.1){
x[1]<-3
x[10]<-5
}
#형식예시;
if( ){
}else if{
}else if{
}else{
}
if(test){
...true statements...
} else {
...false statements...
}
myplus <- function(x, y){
n1 <- length(x)
n2 <- length(y)
if(n1 > n2){
z <- x[1:n2] + y
} else { z <- x + y[1:n1] }
z
}
myplus(1:10, 1:3)
#5.3.2 반복구문
for(k in c(3,7,10))
{
print(k)
}
#for=동일한 연산을 규칙적으로 반복연산,k=변하는값, in하고 "set(값이 변하는 범위)"등장
#print(k) k를 연산하고 돌아가서 7, 10 차례로 연산
#형태
for(i in for_object){
some expressions
}
while(condition){
some expressions
}
#while은 컨디션이 만족하면 계속연산 만족안하면 멈춤
x<-runif(10)
Y<-0
for(k in c(3,7,10))
{Y<-Y+x[k]
print(y)
}
x<-runif(5^10)
y<-0
for(k in 1:length(x)){ #length(x) 메트리스의 길이 마지막원소?
y<-y+x[k]
}
print(y)
#이 경우 반복문 보다 sum(x)가 빠르다 어떻게 하면 for을 쓰지 않을 것인가
tmp<-0
x<-runif(100)
k<-1
while(tmp<10)
{tmp<-tmp+x[k]
k<-k+1
}
#주말에 코딩숙제 이캠퍼스자료실에 업로드
'프로그래밍, 통계학 > R(데이터마이닝)' 카테고리의 다른 글
14.4.1 데이터마이닝 (0) | 2014.04.01 |
---|---|
14.3..27 데이터마이닝 (0) | 2014.03.27 |
14.3.25 R 퀴즈 (0) | 2014.03.25 |
2014.3.13.목.데이터마이닝 (0) | 2014.03.13 |
2014.3.11.데이터마이닝수업 (0) | 2014.03.13 |