반응형
(8) 문자열 (Strings)


R에서 쌍따옴표나 작은 따옴표로 둘러쌓인것은 문자열로 취급된다. 


문자열을 만들 때 주의할점

  • 문자열은 만들 때, 쌍따옴표 혹은 작은따옴표만을 사용하여야한다. 두개를 혼합하여 사용하면 안된다.
  • 쌍따옴표는 작은따옴표 사이에 들어갈 수 잇다.
  • 작은 따옴표는 쌍따옴표 사이에 들어갈 수 있다.
  • 쌍따옴표는 쌍따옴표 사이에 들어갈 수 없다.
  • 작은따옴표는 작은따옴표 사이에 들어갈 수 없다.

적절한 문자열들의 예

a <- 'Start and end with single quote'
print(a)

b <- "Start and end with double quotes"
print(b)

c <- "single quote ' in between double quotes"
print(c)

d <- 'Double quotes " in between single quote'
print(d)


[1] "Start and end with single quote"
[1] "Start and end with double quotes"
[1] "single quote ' in between double quotes"
[1] "Double quotes \" in between single quote"


부적절한 문자열들의 예

e <- 'Mixed quotes" 
print(e)

f <- 'Single quote ' inside single quote'
print(f)

g <- "Double quotes " inside double quotes"
print(g)


문자열 조작(manipulation)

1. paste()

설명 : 두 개의 문자열을 연결(Concatenating)할 때 사용한다.
문법 : paste(..., sep = " ", collapse = NULL)

예 : 

a <- "Hello"
b <- 'How'
c <- "are you? "

print(paste(a,b,c))

print(paste(a,b,c, sep = "-"))

print(paste(a,b,c, sep = "", collapse = ""))

[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "

2. format()

설명 : 문자나 숫자를 원하는 포맷으로 변경한다.

문법 : format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))

  • x : 벡터 input
  • digits : 출력할 총 자릿수
  • nsmall : 소숫점 우측의 최소 자릿수
  • scientific : TRUE이면 scientific notation을 함
  • width : 최소 width (왼쪽에 blank를 넣어 width를 맞춘다.)
  • justify : 정렬할 위치 (좌측정렬, 가운데 정렬, 우측정렬)

# Total number of digits displayed. Last digit rounded off.
result <- format(23.123456789, digits = 9)
print(result)

# Display numbers in scientific notation.
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)

# The minimum number of digits to the right of the decimal point.
result <- format(23.47, nsmall = 5)
print(result)

# Format treats everything as a string.
result <- format(6)
print(result)

# Numbers are padded with blank in the beginning for width.
result <- format(13.7, width = 6)
print(result)

# Left justify strings.
result <- format("Hello", width = 8, justify = "l")
print(result)

# Justfy string with center.
result <- format("Hello", width = 8, justify = "c")
print(result)

[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] "  13.7"
[1] "Hello   "
[1] " Hello  "

3. nchar()

설명 : 문자의 갯수를 세준다. (공백도 포함한다.)
예 :

result <- nchar("Count the number of characters")
print(result)

[1] 30


4. toupper(), tolower()

설명 : 문자열을 대문자로 바꾼다 / 문자열을 모두 소문자로 바꾼다.

생략 

5. substring()

설명 : 문자열의 일부를 추출한다.
문법 : substring(x,first,last)
예 : 

# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)

[1] "act"

아래 튜토리얼을 참고한 포스팅입니다. 

https://www.tutorialspoint.com/r/r_operators.htm 



반응형

'Tools > R' 카테고리의 다른 글

R 통계 분석 - 평균, 중앙값, 최빈값 (Mean, Median, Mode)  (0) 2017.02.23
R - (9) 벡터  (0) 2017.02.23
R을 통한 2017년 대선 주자들 페이스북 분석  (5) 2017.02.21
R - (7) 함수  (0) 2017.02.19
R - (6) 반복문  (0) 2017.02.17
반응형