##Creating lists of vector
farmer.name = c("Luna","Kleen","Ningguang","Keqing","Sucrose","Barbara","Diluc","Kaeya","Lisa","Bennett")
item=c("Fowl","Crab","Sweet Flower","Radish","Mint","Smoked Ham","Shrimp Meat","Cream","Valberry","Glaze Lily")
weight=c(11,4,7,35,217,6,20,10,29,18)
unit.price=c(80,250,50,30,8,180,300,15,5,20)
total.payout=weight*unit.price
##Creating data frame on Wholesale Market
wholesale_df=data.frame(farmer.name,item,weight,unit.price,total.payout)
wholesale_df
## farmer.name item weight unit.price total.payout
## 1 Luna Fowl 11 80 880
## 2 Kleen Crab 4 250 1000
## 3 Ningguang Sweet Flower 7 50 350
## 4 Keqing Radish 35 30 1050
## 5 Sucrose Mint 217 8 1736
## 6 Barbara Smoked Ham 6 180 1080
## 7 Diluc Shrimp Meat 20 300 6000
## 8 Kaeya Cream 10 15 150
## 9 Lisa Valberry 29 5 145
## 10 Bennett Glaze Lily 18 20 360
#adding single row to data frame
new.farmer = data.frame("Jean","Potato",38,12,38*12)
names(new.farmer)=c("farmer.name","item","weight","unit.price","total.payout")
wholesale_df=rbind(wholesale_df,new.farmer)
wholesale_df
## farmer.name item weight unit.price total.payout
## 1 Luna Fowl 11 80 880
## 2 Kleen Crab 4 250 1000
## 3 Ningguang Sweet Flower 7 50 350
## 4 Keqing Radish 35 30 1050
## 5 Sucrose Mint 217 8 1736
## 6 Barbara Smoked Ham 6 180 1080
## 7 Diluc Shrimp Meat 20 300 6000
## 8 Kaeya Cream 10 15 150
## 9 Lisa Valberry 29 5 145
## 10 Bennett Glaze Lily 18 20 360
## 11 Jean Potato 38 12 456
#adding multiple rows using Vector
new.farmers=data.frame(c("Venti","Amber","ZhongLi"),c("Mushroom","Apple","Cabbage"),c(55,20,37),c(13,5,25),c(55*13,20*5,37*25))
names(new.farmers)=c("farmer.name","item","weight","unit.price","total.payout")
wholesale_df=rbind(wholesale_df,new.farmers)
wholesale_df
## farmer.name item weight unit.price total.payout
## 1 Luna Fowl 11 80 880
## 2 Kleen Crab 4 250 1000
## 3 Ningguang Sweet Flower 7 50 350
## 4 Keqing Radish 35 30 1050
## 5 Sucrose Mint 217 8 1736
## 6 Barbara Smoked Ham 6 180 1080
## 7 Diluc Shrimp Meat 20 300 6000
## 8 Kaeya Cream 10 15 150
## 9 Lisa Valberry 29 5 145
## 10 Bennett Glaze Lily 18 20 360
## 11 Jean Potato 38 12 456
## 12 Venti Mushroom 55 13 715
## 13 Amber Apple 20 5 100
## 14 ZhongLi Cabbage 37 25 925
#adding column using R Package
library(tibble)
newwholesale_df=wholesale_df %>% add_column(recipient="WJ")
newwholesale_df
## farmer.name item weight unit.price total.payout recipient
## 1 Luna Fowl 11 80 880 WJ
## 2 Kleen Crab 4 250 1000 WJ
## 3 Ningguang Sweet Flower 7 50 350 WJ
## 4 Keqing Radish 35 30 1050 WJ
## 5 Sucrose Mint 217 8 1736 WJ
## 6 Barbara Smoked Ham 6 180 1080 WJ
## 7 Diluc Shrimp Meat 20 300 6000 WJ
## 8 Kaeya Cream 10 15 150 WJ
## 9 Lisa Valberry 29 5 145 WJ
## 10 Bennett Glaze Lily 18 20 360 WJ
## 11 Jean Potato 38 12 456 WJ
## 12 Venti Mushroom 55 13 715 WJ
## 13 Amber Apple 20 5 100 WJ
## 14 ZhongLi Cabbage 37 25 925 WJ
##Change the titles of the data frame
colnames(newwholesale_df)=c("Farmer Name","Item","Weight","Price per Kg","Total Payout","Received by")
newwholesale_df
## Farmer Name Item Weight Price per Kg Total Payout Received by
## 1 Luna Fowl 11 80 880 WJ
## 2 Kleen Crab 4 250 1000 WJ
## 3 Ningguang Sweet Flower 7 50 350 WJ
## 4 Keqing Radish 35 30 1050 WJ
## 5 Sucrose Mint 217 8 1736 WJ
## 6 Barbara Smoked Ham 6 180 1080 WJ
## 7 Diluc Shrimp Meat 20 300 6000 WJ
## 8 Kaeya Cream 10 15 150 WJ
## 9 Lisa Valberry 29 5 145 WJ
## 10 Bennett Glaze Lily 18 20 360 WJ
## 11 Jean Potato 38 12 456 WJ
## 12 Venti Mushroom 55 13 715 WJ
## 13 Amber Apple 20 5 100 WJ
## 14 ZhongLi Cabbage 37 25 925 WJ
##showcase the structure of data frame
str(newwholesale_df)
## 'data.frame': 14 obs. of 6 variables:
## $ Farmer Name : chr "Luna" "Kleen" "Ningguang" "Keqing" ...
## $ Item : chr "Fowl" "Crab" "Sweet Flower" "Radish" ...
## $ Weight : num 11 4 7 35 217 6 20 10 29 18 ...
## $ Price per Kg: num 80 250 50 30 8 180 300 15 5 20 ...
## $ Total Payout: num 880 1000 350 1050 1736 ...
## $ Received by : chr "WJ" "WJ" "WJ" "WJ" ...
##Exercising different ways R returns**
names(newwholesale_df) #showing the title of each column
## [1] "Farmer Name" "Item" "Weight" "Price per Kg" "Total Payout"
## [6] "Received by"
head(newwholesale_df) #showing first 6 rows of the data frame
## Farmer Name Item Weight Price per Kg Total Payout Received by
## 1 Luna Fowl 11 80 880 WJ
## 2 Kleen Crab 4 250 1000 WJ
## 3 Ningguang Sweet Flower 7 50 350 WJ
## 4 Keqing Radish 35 30 1050 WJ
## 5 Sucrose Mint 217 8 1736 WJ
## 6 Barbara Smoked Ham 6 180 1080 WJ
tail(newwholesale_df) #showing last 6 rows of the data frame
## Farmer Name Item Weight Price per Kg Total Payout Received by
## 9 Lisa Valberry 29 5 145 WJ
## 10 Bennett Glaze Lily 18 20 360 WJ
## 11 Jean Potato 38 12 456 WJ
## 12 Venti Mushroom 55 13 715 WJ
## 13 Amber Apple 20 5 100 WJ
## 14 ZhongLi Cabbage 37 25 925 WJ
summary(newwholesale_df) #summaries each column
## Farmer Name Item Weight Price per Kg
## Length:14 Length:14 Min. : 4.00 Min. : 5.00
## Class :character Class :character 1st Qu.: 10.25 1st Qu.: 12.25
## Mode :character Mode :character Median : 20.00 Median : 22.50
## Mean : 36.21 Mean : 70.93
## 3rd Qu.: 36.50 3rd Qu.: 72.50
## Max. :217.00 Max. :300.00
## Total Payout Received by
## Min. : 100.0 Length:14
## 1st Qu.: 352.5 Class :character
## Median : 797.5 Mode :character
## Mean :1067.6
## 3rd Qu.:1037.5
## Max. :6000.0
newwholesale_df[4:5,] #accessing two sets of data (row no.4 & 5) using matrix
## Farmer Name Item Weight Price per Kg Total Payout Received by
## 4 Keqing Radish 35 30 1050 WJ
## 5 Sucrose Mint 217 8 1736 WJ
newwholesale_df[,2] #accessing data of column no.2 using matrix
## [1] "Fowl" "Crab" "Sweet Flower" "Radish" "Mint"
## [6] "Smoked Ham" "Shrimp Meat" "Cream" "Valberry" "Glaze Lily"
## [11] "Potato" "Mushroom" "Apple" "Cabbage"
newwholesale_df[7,4] #accesing single data from the data frame (the unit price of shrimp meat)
## [1] 300
##additional
##check which farmer's payout is more than 1000
big_payout=newwholesale_df$`Total Payout`>=1000
amount=which(big_payout)
amount
## [1] 2 4 5 6 7
##sum the total no.of farmers
sum(big_payout)
## [1] 5
## or just show all the details about the farmers that has more than 1000 payout.
newwholesale_df[big_payout,]
## Farmer Name Item Weight Price per Kg Total Payout Received by
## 2 Kleen Crab 4 250 1000 WJ
## 4 Keqing Radish 35 30 1050 WJ
## 5 Sucrose Mint 217 8 1736 WJ
## 6 Barbara Smoked Ham 6 180 1080 WJ
## 7 Diluc Shrimp Meat 20 300 6000 WJ