반응형

Programing 145

파일 입출력, R 프로그래밍

/* * http://sosal.kr/ * made by so_Sal */ FILE I/O 일반적으로 R programming의 작업 환경은 내문서에서 시작된다. getwd() # 현재 디렉토리 위치를 반환하는 함수 getwd returns an absolute filepath representing the current working directory of the R process; setwd(dir) is used to set the working directory to dir. > getwd()[1] "C:/Users/sosal/Documents" setwd() # 현재 디렉토리 위치를 바꾸는 함수 윈도우즈의 경우 파일-> 직업 디렉토리 변경기능을 통해 GUI로 설정할 수 있다. > help(get..

데이터 추출 및 병합 연산 R프로그래밍

/* * http://sosal.kr/ * made by so_Sal */ 실습데이터)> weight height gender testDate patients patients.sub patients.sub weight height1 65.4 1702 55.0 1553 380.0 NA4 72.2 1735 51.0 1616 NA 166 1. subset (부분집합) patients 변수에서 몸무게가 60키로 이상히고, 성별이 남자인 경우를 추출하는 경우 - 조건문을 사용하는 경우> patients[ (patients$weight > 60 & patients$gender=='M'), ] weight height gender testDate1 65.4 170 M 2013-09-013 380.0 NA M 2013..

apply 함수군 (lapply, sapply, tapply) R 프로그래밍

/* * http://sosal.kr/ * made by so_Sal */ apply() 함수는 벡터, 행렬 등의 데이터 프레임에서 row/column 단위의 계산을 할 때 함수를 쉽게 적용할 수 있도록 도와준다. apply 함수 외에 리스트, 벡터, 테이블 형태로 결과를 반환해주는 lapply, sapply, tapply 함수에 대해 알아보자. 실습데이터) > weight height gender testDate patients patients.sub patients.sub weight height 1 65.4 170 2 55.0 155 3 380.0 NA 4 72.2 173 5 51.0 161 6 NA 166 - 각 환자(row)별로 몸무게와 키의 평균을 구한다 > apply(patients.sub,..

결측치 NA (Missing value) 연산 / R programming

/* * http://sosal.kr/ * made by so_Sal */ R 프로그래밍에서 결측지(missing value)는 NA (Not Available) 라는 문자로 처리해야 한다.NaN (Not a Number)는 분모를 0으로 나누는 것과 같이 계산이 불가능 할 경우 출력되는 문자다. > y y[1] 1 2 3 NA is.na()는 벡터의 결측지가 존재할 경우 true> is.na(y)[1] FALSE FALSE FALSE TRUE> > summary(y) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's 1.0 1.5 2.0 2.0 2.5 3.0 1 > NA는 missing value를 표현하는 논리형 자료이지만, "NA"는 문자열 그 자체이다.> is.na( N..

R프로그래밍 범주형 변수 factor (categorical variable)

/* * http://sosal.kr/ * made by so_Sal */ factor는 R에서 제공하는 categorical variable(범주형 변수)로, 여러개의 level로 구성된다. 혈액형이라는 범주형 변수가 존재할 때, A,B,AB,O 라는 level을 가지게 된다. > BloodType summary(BloodType) Length Class Mode 10 character character 위에서 정의한 BloodType이라는 vector를 factor로 형변한 하기 위해서 factor() 함수를 사용한다. Description The function factor is used to encode a vector as a factor (the terms ‘category’ and ‘enum..

벡터 및 행렬의 row/column에 이름 붙이기

/* * http://sosal.kr/ * made by so_Sal */ names(vector)rownames(matrix)colnames(matrix) 1. 벡터에 이름 붙이기 > x names(x)NULL > names(x) xA B C D E 1 2 3 4 5 > x['C']C 3 > names(x)[1] "A" "B" "C" "D" "E"> 2. 매트릭스에 이름 붙이기 > CountTable CountTable [,1] [,2][1,] 189 10845[2,] 104 10933> > rownames(CountTable) colnames(CountTable) CountTable No heart attack Heart attackPlacebo 189 10845Aspirin 104 10933> > ..

R프로그래밍에서 matrix 다루기

/* * http://sosal.kr/ * made by so_Sal */ 1. Matrix 선언하기 matrix( data, nrow, ncol, byrow = FALSE) data: 행렬에 들어가는 데이터nrow: row(행)의 수ncol: column(열)의 수byrow: 기본적으로 열(column)을 기준으로 숫자가 들어감 (FALSE) TRUE로 지정할 시 행(row) 기준으로 숫자가 들어간다. > mat = matrix(1:20, nrow=5, ncol=4)> mat [,1] [,2] [,3] [,4][1,] 1 6 11 16[2,] 2 7 12 17[3,] 3 8 13 18[4,] 4 9 14 19[5,] 5 10 15 20> 행열 요소 추출, 치환 > mat[1,1][1] 1> mat[, ..

R프로그래밍에서 Vector 다루기

/* * http://sosal.kr/ * made by so_Sal */ 1. 벡터 생성하기 mode( base )DescriptionGet or set the type or storage mode of an object. c( items....) 로 벡터를 생성할 수 있습니다.mode( base ) 로 벡터의 구성을 알 수 있습니다. 1. 모두 Int형인경우: int로 나옵니다.> x x[1] 23 34 44> mode(x)[1] "numeric" 2. 데이터가 여러개인 경우: 하나로 묶을 수 있는 데이터 타입으로 변환> x x[1] "23" "44" "hello world"> mode(x)[1] "character" 2. 벡터 연산: 모든 데이터에 대해 동일한 연산을 하는 방법입니다.> weight..

Ubuntu에서 Julia 개발환경 설치하기

/* * http://sosal.kr/ * made by so_Sal */ Dynamic programming language인 Julia 설치법입니다. http://julialang.org/downloads/ 들어가시면 간단하게 다 설명이 되어있지만, 아직 한국 블로거들 사이에서는 언급이 거의 안되는 언어라 한번 포스팅 해봅니다. UbuntuA PPA (Personal Package Archive) is provided for Ubuntu systems to allow for automatic updating to the latest stable version of Julia. To use this PPA and install julia on Ubuntu 12.04 or later, run the fo..

반응형