#Create a data frame
df <- data.frame(
fruits = c('Lemons', 'Plums', 'Papayas', 'Coconuts', 'Mangoes', 'Pears'),
weight_gr = c(200, 300, 150, 400, 600, 750),
date = c('2023/7/01','2023/7/02','2023/7/05','2023/7/07','2023/7/10','2023/7/11')
)
df
## fruits weight_gr date
## 1 Lemons 200 2023/7/01
## 2 Plums 300 2023/7/02
## 3 Papayas 150 2023/7/05
## 4 Coconuts 400 2023/7/07
## 5 Mangoes 600 2023/7/10
## 6 Pears 750 2023/7/11
#summarize the data
summary(df)
## fruits weight_gr date
## Length:6 Min. :150 Length:6
## Class :character 1st Qu.:225 Class :character
## Mode :character Median :350 Mode :character
## Mean :400
## 3rd Qu.:550
## Max. :750
#Add a new row
new_df <- rbind(df, c('Watermelon', 250, '2023/7/16'))
new_df
## fruits weight_gr date
## 1 Lemons 200 2023/7/01
## 2 Plums 300 2023/7/02
## 3 Papayas 150 2023/7/05
## 4 Coconuts 400 2023/7/07
## 5 Mangoes 600 2023/7/10
## 6 Pears 750 2023/7/11
## 7 Watermelon 250 2023/7/16
#Add columns
new_df <- cbind(new_df, category = c(1, 2, 3, 4, 5, 6, 7))
new_df
## fruits weight_gr date category
## 1 Lemons 200 2023/7/01 1
## 2 Plums 300 2023/7/02 2
## 3 Papayas 150 2023/7/05 3
## 4 Coconuts 400 2023/7/07 4
## 5 Mangoes 600 2023/7/10 5
## 6 Pears 750 2023/7/11 6
## 7 Watermelon 250 2023/7/16 7
#Remove rows and columns
r_new_df <- new_df[-c(1), -c(1)]
r_new_df
## weight_gr date category
## 2 300 2023/7/02 2
## 3 150 2023/7/05 3
## 4 400 2023/7/07 4
## 5 600 2023/7/10 5
## 6 750 2023/7/11 6
## 7 250 2023/7/16 7
#Access items
df[1]
## fruits
## 1 Lemons
## 2 Plums
## 3 Papayas
## 4 Coconuts
## 5 Mangoes
## 6 Pears
df[c(1,3)]
## fruits date
## 1 Lemons 2023/7/01
## 2 Plums 2023/7/02
## 3 Papayas 2023/7/05
## 4 Coconuts 2023/7/07
## 5 Mangoes 2023/7/10
## 6 Pears 2023/7/11
df[-c(1)]
## weight_gr date
## 1 200 2023/7/01
## 2 300 2023/7/02
## 3 150 2023/7/05
## 4 400 2023/7/07
## 5 600 2023/7/10
## 6 750 2023/7/11
df["weight_gr"]
## weight_gr
## 1 200
## 2 300
## 3 150
## 4 400
## 5 600
## 6 750
df[["weight_gr"]]
## [1] 200 300 150 400 600 750
df[["date"]]
## [1] "2023/7/01" "2023/7/02" "2023/7/05" "2023/7/07" "2023/7/10" "2023/7/11"
df$date
## [1] "2023/7/01" "2023/7/02" "2023/7/05" "2023/7/07" "2023/7/10" "2023/7/11"
#Amount of rows and columns
dim(new_df)
## [1] 7 4
cat("nrow: ", nrow(new_df), "and ncol: ", ncol(new_df))
## nrow: 7 and ncol: 4
#Data frame length
length(new_df)
## [1] 4
#Combining data frames
df1 <- data.frame(
fruits = c('Lemons', 'Plums', 'Papayas'),
weight_gr = c(200, 300, 150)
)
df2 <- data.frame(
fruits = c('Coconuts', 'Mangoes', 'Pears'),
weight_gr = c(400, 600, 750)
)
row_com_df <- rbind(df1, df2)
row_com_df
## fruits weight_gr
## 1 Lemons 200
## 2 Plums 300
## 3 Papayas 150
## 4 Coconuts 400
## 5 Mangoes 600
## 6 Pears 750
df3 <- data.frame(
fruits = c('Lemons', 'Plums', 'Paypayas'),
weight_gr = c(200, 300, 150)
)
df4 <- data.frame(
category = c(1, 2, 3),
buy = c(TRUE, FALSE, FALSE)
)
col_com_df <- cbind(df3, df4)
col_com_df
## fruits weight_gr category buy
## 1 Lemons 200 1 TRUE
## 2 Plums 300 2 FALSE
## 3 Paypayas 150 3 FALSE
summary(col_com_df)
## fruits weight_gr category buy
## Length:3 Min. :150.0 Min. :1.0 Mode :logical
## Class :character 1st Qu.:175.0 1st Qu.:1.5 FALSE:2
## Mode :character Median :200.0 Median :2.0 TRUE :1
## Mean :216.7 Mean :2.0
## 3rd Qu.:250.0 3rd Qu.:2.5
## Max. :300.0 Max. :3.0