티스토리 뷰

R

(macOS)[R] paste, substr, as., date, format

jinozpersona 2022. 3. 28. 17:13

INTRO

1. paste, substr

2. as. : 객체변환

3. date, format

 

 

1. paste, substr

paste

 입력받은 문자를 하나로 붙여주며 "sep=" 옵션을 이용해 문자열들 사이에 구분자(separator) 사입 가능

 

substr

 문자열에서 특정 문자열을 추출하는 기능

 

test_etc_fun.R
rm(list=ls())
setwd = "~/Rcoding"

## paste
number = 1:10
alphabet = c('a','b','c')
paste(number,alphabet)
paste(number,alphabet,sep=" to the ")


## substr
substr("BigDataAnalysis", 1,4)
contrys = c('Korea', 'Japan', 'China', 'Singapore', 'Russia')
substr(contrys, 1,3)

출력결과

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

> rm(list=ls())

> setwd = "~/Rcoding"

> ## paste
> number = 1:10

> alphabet = c('a','b','c')

> paste(number,alphabet)
 [1] "1 a"  "2 b"  "3 c"  "4 a"  "5 b"  "6 c"  "7 a"  "8 b"  "9 c"  "10 a"

> paste(number,alphabet,sep=" to the ")
 [1] "1 to the a"  "2 to the b"  "3 to the c"  "4 to the a"  "5 to the b"  "6 to the c"  "7 to the a" 
 [8] "8 to the b"  "9 to the c"  "10 to the a"

> ## substr
> substr("BigDataAnalysis", 1,4)
[1] "BigD"

> contrys = c('Korea', 'Japan', 'China', 'Singapore', 'Russia')

> substr(contrys, 1,3)
[1] "Kor" "Jap" "Chi" "Sin" "Rus"

 

2. as.

자료형 데이터 구조 변환

as.data.frame(x) : 데이터 프레임 형식으로 변환

as.list(x) : 리스트 형식으로 변환

as.matrix(x) : 행렬 형식으로 변환

as.vector(x) : 벡터 형식으로 변환

as.factor(x) : 팩터(factor) 형식으로 변환

 

test_etc_fun.R
## as.
as.integer(3.14)
#as.numeric("foo")
as.character(101)
as.numeric(FALSE)
as.logical(0.45)

##as.matrix
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)
as.matrix(mydat)

 

출력결과

> ## as.
> as.integer(3.14)
[1] 3

> #as.numeric("foo") : 잘못된 형식, Error N/A
> as.character(101)
[1] "101"

> as.numeric(FALSE)
[1] 0

> as.logical(0.45)
[1] TRUE

> ##as.matrix
> 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

> as.matrix(mydat)
     income car       marriage
[1,] "100"  "kia"     "FALSE" 
[2,] "200"  "hyundai" "FALSE" 
[3,] "150"  "kia"     "FALSE" 
[4,] "300"  "toyota"  "TRUE"  
[5,] "900"  "lexus"   "TRUE"

 

 

3.  date, format

문자열 날짜 변환과 format을 이용한 세부 변환

## date, format
as.Date("2022-03-29")
#as.Date("03/29/2022") : 잘못된 형식, Error N/A

as.Date("03/29/2022",format="%m/%d/%y")
format(Sys.Date())
as.character(Sys.Date())
format(Sys.Date(),format="%m/%d/%y")
format(Sys.Date(),format="%y.%m.%d")
format(Sys.Date(), "%a")
format(Sys.Date(), "%b")
format(Sys.Date(), "%Y")
format(Sys.Date(),format="%a, %d.%b.%Y")

 

출력결과

> ## date, format
> as.Date("2022-03-29")
[1] "2022-03-29"

> #as.Date("03/29/2022")
> 
> as.Date("03/29/2022",format="%m/%d/%y")
[1] "2020-03-29"

> format(Sys.Date())
[1] "2022-03-29"

> as.character(Sys.Date())
[1] "2022-03-29"

> format(Sys.Date(),format="%m/%d/%y")
[1] "03/29/22"

> format(Sys.Date(),format="%y.%m.%d")
[1] "22.03.29"

> format(Sys.Date(), "%a")
[1] "Tue"

> format(Sys.Date(), "%b")
[1] "Mar"

> format(Sys.Date(), "%Y")
[1] "2022"

> format(Sys.Date(),format="%a, %d.%b.%Y")
[1] "Tue, 29.Mar.2022"

 

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함