티스토리 뷰

R

(macOS)[R] 데이터 다루기(data handle)

jinozpersona 2022. 3. 28. 14:21

INTRO

1. 벡터형 변수

2. 행렬/데이터프레임

 

 

test_indexing.R
rm(list = ls())
setwd("/Users/[YourMacID]/Rcoding")

a_chr = c('a', 'b', 'c', 'd', 'e')

## vector indexing
a_chr[1]
a_chr[-1]
a_chr[c(2,4)]
a_chr[1:3]

## dataframe indexing
income = c(100,200,150,300,900)
car = c('kia','hyundai','kia','toyota','lexus')
marriage=c(FALSE,FALSE,FALSE,TRUE,TRUE)
mydat = data.frame(income,car,marriage)
print(mydat)

mydat[3,2]
mydat[,2]
mydat[3,]

 

출력결과

> source("~/Rcoding/test_indexing.R", echo=TRUE)

> rm(list = ls())

> setwd("/Users/[YourMacID]/Rcoding")

> a_chr = c('a', 'b', 'c', 'd', 'e')

> ## vector indexing
> a_chr[1]
[1] "a"

> a_chr[-1]
[1] "b" "c" "d" "e"

> a_chr[c(2,4)]
[1] "b" "d"

> a_chr[1:3]
[1] "a" "b" "c"

> ## dataframe indexing
> income = c(100,200,150,300,900)

> car = c('kia','hyundai','kia','toyota','lexus')

> marriage=c(FALSE,FALSE,FALSE,TRUE,TRUE)

> mydat = data.frame(income,car,marriage)

> print(mydat)
  income     car marriage
1    100     kia    FALSE
2    200 hyundai    FALSE
3    150     kia    FALSE
4    300  toyota     TRUE
5    900   lexus     TRUE

> mydat[3,2]
[1] "kia"

> mydat[,2]
[1] "kia"     "hyundai" "kia"     "toyota"  "lexus"  

> mydat[3,]
  income car marriage
3    150 kia    FALSE

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함