Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Data Extraction | Data Frames
R Introduction: Part II

book
Data Extraction

Finish the section with practice on abstract local furniture store selling data!

Tarefa

Swipe to start coding

You will work with the data below. This data is stored in the store variable.

ItemPriceSold
Sofa34067
Armchair15081
Dining table11579
Bookshelf16042
Kitchen Cabinet7067

Your tasks are:

  1. Add a new column named 'Revenue' with the revenue of each item (multiply 'Price' and 'Sold'). To access columns, use the $ sign.
  2. Using the sum() function, calculate the total number of items sold. Save in items_sold variable and output its value.
  3. Calculate total revenue using sum() function. Save in the total_revenue variable and output its value.
  4. Find out the average price of the sold item - divide total_revenue by items_sold.

Solução

# Initial data frame
prices <- c(340, 150, 115, 160, 70)
items <- c('Sofa', 'Armchair', 'Dining table',
'Bookshelf', 'Kitchen Cabinet')
sold <- c(67, 81, 79, 42, 67)
store <- data.frame(items, prices, sold)
colnames(store) <- c("Item", "Price", "Sold")

# Add new column
store$Revenue <- store$Sold * store$Price
# Total number of items sold
items_sold <- sum(store$Sold)
items_sold
# Total revenue
total_revenue <- sum(store$Revenue)
total_revenue
# Average price of sold item
total_revenue/items_sold

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 7
# Initial data frame
prices <- c(340, 150, 115, 160, 70)
items <- c('Sofa', 'Armchair', 'Dining table',
'Bookshelf', 'Kitchen Cabinet')
sold <- c(67, 81, 79, 42, 67)
store <- data.frame(items, prices, sold)
colnames(store) <- c("Item", "Price", "Sold")

# Add new column
___ <- store$Sold * ___
# Total number of items sold
___ <- ___(store$___)
___
# Total revenue
___ <- sum(___)
___
# Average price of sold item
___/___

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt