R에서 원시 데이터를 수동으로 입력하는 방법
R은 데이터 작업에 가장 널리 사용되는 프로그래밍 언어 중 하나입니다. 하지만 데이터 작업을 하기 전에 실제로 데이터를 R로 가져와야 합니다!
데이터가 이미 CSV 또는 Excel 파일에 있는 경우 다음 튜토리얼의 단계에 따라 이를 R로 가져올 수 있습니다.
그러나 때로는 원시 데이터를 R에 수동으로 입력하고 싶을 수도 있습니다. 이 튜토리얼에서는 이를 수행하는 방법을 설명합니다.
벡터를 입력하세요.
다음 구문을 사용하여 숫자 값의 단일 벡터를 R에 입력할 수 있습니다.
#create vector of numeric values numeric_values <- c(1, 3, 5, 8, 9) #display class of vector class(numeric_values) [1] "digital" #display vector of numeric values numeric_values [1] 1 3 5 8 9 #return second element in vector numeric_values[4] [1] 8
동일한 구문을 사용하여 문자 값의 벡터를 입력할 수 있습니다.
#create vector of character values char_values <- c("Bob", "Mike", "Tony", "Andy") #display class of vector class(char_values) [1] “character”
데이터 블록을 입력하세요
다음 구문을 사용하여 값이 포함된 데이터 프레임을 R에 입력할 수 있습니다.
#create data frame
df <- data.frame(team=c("A", "A", "B", "B", "C"),
dots=c(12, 15, 17, 24, 27),
assists=c(4, 7, 7, 8, 12))
#display data frame
df
team points assists
1 to 12 4
2 to 15 7
3 B 17 7
4 B 24 8
5 C 27 12
#display class of df
class(df)
[1] "data.frame"
#return value in fourth row and third column
df[4, 3]
[1] 8
행렬을 입력하세요
다음 구문을 사용하여 R에 값 행렬을 입력할 수 있습니다.
#create matrix with two columns and five rows
dots=c(12, 15, 17, 24, 27)
assists=c(4, 7, 7, 8, 12)
#column bind the two vectors together to create a matrix
mat <- cbind(points, assists)
#display matrix
mast
assist points
[1,] 12 4
[2,] 15 7
[3,] 17 7
[4,] 24 8
[5,] 27 12
#display class of mat
class(mat)
[1] "matrix"
#return value in fourth row and second column
mat[4, 2]
assists
8
참고: 행렬에서는 데이터 프레임과 달리 각 열이 동일한 유형이어야 합니다.
여기에서 더 많은 R 튜토리얼을 찾을 수 있습니다.