0

I have a completed working script, however i would like to add additional rows to the script. Same error happens in colnames additions

adding the new ones & redirecting to fresh sheet

colnames(main)<-c("Company","Mapped","Not.Mapped","Pending")
rownames(main)<-c("CompA","CompB","CompC")

write.table(main, file="Main.csv", sep=",", row.names = FALSE)

Error in .rowNamesDF<-(x, value = value) : invalid 'row.names' length

output should look like the below

Company Mapped  Not.Mapped  Pending X.Mapped  
CompA   190     19          63      90.91%  
CompB                 
CompC   66      9           36      88.00%```
aynber
  • 22,380
  • 8
  • 50
  • 63
HP.
  • 37
  • 5
  • 2
    You are changing the column and row names with these calls. Not adding new rows to a data.frame. – phiver Sep 13 '19 at 14:19
  • it seemed to work the first time i created this script. How would i add new rows, and how come it worked previously? Thanks Phiver – HP. Sep 13 '19 at 14:27
  • 1
    Possible duplicate of [Add row to dataframe](https://stackoverflow.com/questions/28467068/add-row-to-dataframe) – phiver Sep 13 '19 at 14:33
  • See link to duplicate. I don't think it worked previously as you expected it to work. – phiver Sep 13 '19 at 14:35
  • i want to change the row names, by adding "CompB" in as a new static row. This will then give blanks on the columns (other than company) where i can info afterwards. – HP. Sep 13 '19 at 14:48

1 Answers1

0

I think your question is not clear at all. If you want to add a new row you can use []. Note that dim(main)[1] returns the total rows in your current object. dim(main)[1] + 1 indicates that you are going to add an additional row. The vector you pass must have 4 elements (or as many variables your dataframe have)

main[dim(main)[1] + 1,] <- c(1,2,3,4)
yarnabrina
  • 1,561
  • 1
  • 10
  • 30
Orlando Sabogal
  • 1,470
  • 7
  • 20