49

I'm trying to create a plot from elements of csv file which looks like this:

h1,h2,h3,h4
a,1,0,1,0
b,1,1,0,1
c,0,0,1,0

I tried the following code but am receiving an error saying

Error in data.frame(id = varieties, attr(mat, "row.names"), check.rows = FALSE) : arguments imply differing number of rows: 8, 20

My sample data has 8 columns and 20 rows (excluding header and row names). I tried to look up online and tried to implement a few fixes but the issue still persists. I'd really appreciate any help.

mat <- read.csv("trial.csv", header=T, row.names=1)
varieties = names(mat)
df <- data.frame(id=varieties,attr(mat, "row.names"), check.rows= FALSE)
UseR10085
  • 7,120
  • 3
  • 24
  • 54
abn
  • 1,353
  • 4
  • 28
  • 58
  • Which line throws the error? Can you run the script line-by-line and report the findings? Is it possible to share the file? – Myles Baker Oct 01 '14 at 17:59
  • @MylesBaker 'df <- data.frame(id=varieties,attr(mat, "row.names"), check.rows= FALSE)' gives the error. – abn Oct 01 '14 at 18:00
  • 1
    This will only work if `mat` is a square matrix (nrows = ncols). What do you want to achieve? Maybe a `list` is better suited for your needs... Or see `cbind.fill` [here](http://stackoverflow.com/questions/7962267/cbind-a-df-with-an-empty-df-cbind-fill) – EDi Oct 01 '14 at 18:10
  • I'd like to create a plot for a rectangular matrix, showing how each element is distributed across the data @EDi – abn Oct 01 '14 at 18:13
  • Maybe you are looking for this `require(reshape2); mat$id <- rownames(mat); melt(mat)` – EDi Oct 01 '14 at 18:15

3 Answers3

34

Your data.frame mat is rectangular (n_rows!= n_cols).

Therefore, you cannot make a data.frame out of the column- and rownames, because each column in a data.frame must be the same length.

Maybe this suffices your needs:

require(reshape2)
mat$id <- rownames(mat) 
melt(mat)
EDi
  • 13,160
  • 2
  • 48
  • 57
  • 12
    Why exactly is this an issue? I've never had problems with rectangular data frames. Honestly I doubt any of them ever had the same amount of rows as columns. – B.Quaink May 24 '20 at 19:09
17

I had the same error message so I went googling a bit I managed to fix it with the following code.

df<-data.frame(words = unlist(words))

words is a character list.

This just in case somebody else needs the output to be a data frame.

Simone
  • 497
  • 5
  • 19
14

Though this isn't a DIRECT answer to your question, I just encountered a similar problem, and thought I'd mentioned it:

I had an instance where it was instantiating a new (no doubt very inefficent) record for data.frame (a result of recursive searching) and it was giving me the same error.

I had this:

return(
  data.frame(
    user_id = gift$email,
    sourced_from_agent_id = gift$source,
    method_used = method,
    given_to = gift$account,
    recurring_subscription_id = NULL,
    notes = notes,
    stringsAsFactors = FALSE
  )
)

turns out... it was the = NULL. When I switched to = NA, it worked fine. Just in case anyone else with a similar problem hits THIS post as I did.