Data Frame
by mervyn
source “K-MOOC 오세종 교수님의 [R 데이터 분석 입문] 강좌의 3-2. data frame 중 (http://www.kmooc.kr/courses/course-v1:DKUK+DKUK0003+2020_T3)”
data frame
Create data frame
city<- c("Seoul", "Tokyo", "Washington")
rank<- c(1, 3, 2)
city.info<- data.frame(city, rank)
We usually call data from file
dataset
iris: dataset used in R
Extract column data from data.frame
is.data.frame(iris) #Is iris data frame?
[1] TURE
- Can be used like matrix
iris[,"Species"] # the result is a horizontal (1D) vector
iris["Species"] # column direction: data frame of 150 row and 1 column
iris$Species # same result with iris[, "Species"]. Cannot be used for matrix, needed to converted
Commands for both data frame and matrix
iris[, c(1:2)] # 1st, 2nd columns. data frame
iris[, c(1, 3, 5)] # combine col 1, 3, 5 data frame
iris[, c("Sepal.Length", "Species")] # bring 2 columns Sepal.Length and Species
iris[1:50,] # cut row 1 to row 50
iris[1:50, c(1,3)] # cut row 1 to row 50, and cut column 1 and column 3
Assignment
- Create data frame df
>df
id name score
1 10 John 95
2 20 Tom 46
3 30 Paul 98
4 40 Jane 74
5 50 Grace 85
-
Compare df$score, df[,3], df[, ‘score’]
-
Print values of column id and score
-
Print values of row 2, row 3
-
Print the value of row 2, column 3
id<- c(10,20,30,40,50)
name<- c("John", "Tom", "Paul", "Jane", "Grace")
score<- c(95, 46, 98, 74, 85)
df<- data.frame(id, name, score)
df
df$score #same results for df$score, df[,3], and df[,'score']
df[,3]
df[,'score']
df[, c("id", "score")]
df[2:3,]
df[2, 3]
Comments
Post comment