Bruno Avila
Bruno Avila

Reputation: 296

Change Data Frame to Table

I have a DF that I'm trying to make into a table to make the Pareto chart. This DF has two columns (KEY AND PRICE) This is my query:

library (qcc)

row.names (DF) <- DF$KEY #### row name is the same as my key. It is a condition for making the chart.
pareto.chart (DF, cumperc = seq (0,100, by = 5)) ### create graph

When I try to create the graph I get the message: Error in as.table.default (data): cannot coerce to a table

I tried using as.table (as.matrix (DF)), but still as Data.Frame.

How do I turn my DF into a table?

Upvotes: 1

Views: 298

Answers (2)

deepseefan
deepseefan

Reputation: 3791

The input pareto.chart takes is a named vector or named number. Here is an example using a sample DF:

llibrary (qcc)
DF <- data.frame(KEY=c(1,2,3,4,5,6,7,8,9,10), 
                 PRICE = c(7, 5, 10, 15, 20, 100, 7, 100, 100, 80))
# create a vector of PRICE
price <- DF$PRICE
# assign key as name to price
names(price) <- DF$KEY

# plot using price
pareto.chart (price, cumperc = seq (0,100, by = 5), main="Example") ### create graph

Output

pareto_chart_example

Hope that helps.

Upvotes: 2

larsoevlisen
larsoevlisen

Reputation: 313

You will need to pass a named vector to the pareto.chart function:

library(qcc)

df <- data.frame(key = c("Item 1", "Item 2", "Item 3", "Item 4"),
                 price = c(22, 34, 53, 14),
                 stringsAsFactors = FALSE)

data_vec <- df$price # Create a vector of the prices

names(data_vec) <- df$key # Name the vector elements by the 'key' variable in the data frame

pareto.chart(data_vec, ylab = "Price")

Upvotes: 1

Related Questions