1

I have a empty DF:

CharactersDF <- data.frame(matrix(ncol = 7, nrow = 0))

And I created a new class:

 RPGcharacter <- function(name, level, rpgClass, race, HP, attack, resist){
   value <- list(name= name, level = level, rpgClass = rpgClass, HP = HP, attack = attack, resist = resist)
   attr(value, "class") <- "RPGcharacter"
   value
 }

And then new object:

Artur <- RPGcharacter("Artur", 22, "Warlock", "Dwarf", 130, 12, 3)

How to add the object as a row in that empty df? I tried:

   CharactersDF <- rbind(CharactersDF, Artur)

but I got:

 Warning messages:
1: In `[<-.data.frame`(`*tmp*`, 1, value = list(name = "Artur", level = 22,  :
  replacement element 1 has 1 row to replace 0 rows
2: In `[<-.data.frame`(`*tmp*`, 1, value = list(name = "Artur", level = 22,  :
  replacement element 2 has 1 row to replace 0 rows
3: In `[<-.data.frame`(`*tmp*`, 1, value = list(name = "Artur", level = 22,  :
  replacement element 3 has 1 row to replace 0 rows
4: In `[<-.data.frame`(`*tmp*`, 1, value = list(name = "Artur", level = 22,  :
  replacement element 4 has 1 row to replace 0 rows
5: In `[<-.data.frame`(`*tmp*`, 1, value = list(name = "Artur", level = 22,  :
  replacement element 5 has 1 row to replace 0 rows
6: In `[<-.data.frame`(`*tmp*`, 1, value = list(name = "Artur", level = 22,  :
  replacement element 6 has 1 row to replace 0 rows
7: In `[<-.data.frame`(`*tmp*`, 1, value = list(name = "Artur", level = 22,  :
  provided 6 variables to replace 1 variables
martin
  • 1,145
  • 1
  • 7
  • 24
  • 4
    did you mean: CharactersDF <- rbind(CharactersDF, Artur) – Ben Jun 24 '19 at 21:59
  • Hey! Yes, i tried Ur suggestion and got longer error :D Edited. Hmm.. looks like problem with object? – martin Jun 24 '19 at 22:02
  • 2
    What you did must be slightly different than the code & data you shared in your question, bc when I run this code it works fine (calling `rbind` correctly, as in the latest version). – joran Jun 24 '19 at 22:05
  • OK, i had problem with env. All works fine. Thank u guys! – martin Jun 24 '19 at 22:15

2 Answers2

1

As Ben pointed out in his comment, you want to use CharactersDF <- rbind(CharactersDF, Artur).

You are using the rbind function on CharactersDF and Artur, so you use rbind(). You're trying to use a Python-esque syntax where the period means you're calling a property of the object CharactersDF. Here, you want to call rbind() on the two objects and then assign the result wherever you want to store it.

1

Although the rbind solution works, take care with data types and data structures. The empty CharactersDF is a data frame that contains logical types

> CharactersDF <- data.frame(matrix(ncol = 7, nrow = 0))
> str(CharactersDF)
'data.frame':   0 obs. of  7 variables:
 $ X1: logi 
 $ X2: logi 

Artur is a list that has numerical and character types

> Artur <- RPGcharacter("Artur", 22, "Warlock", "Dwarf", 130, 12, 3)
> str(Artur)
List of 6
 $ name    : chr "Artur"
 $ level   : num 22
 $ rpgClass: chr "Warlock"
 $ HP      : num 130
 $ attack  : num 12
 $ resist  : num 3
 - attr(*, "class")= chr "RPGcharacter"

And following rbind the data types change again to numerical values and factors.

> CharactersDF <- rbind(CharactersDF, Artur)
> str(CharactersDF)
'data.frame':   1 obs. of  6 variables:
 $ name    : Factor w/ 1 level "Artur": 1
 $ level   : num 22
 $ rpgClass: Factor w/ 1 level "Warlock": 1
 $ HP      : num 130
 $ attack  : num 12
 $ resist  : num 3 

It would be safer to work out the types you need and work with them consistently, or deliberately coerce them as required.

To create an empty data frame with pre-set types, check Create an empty data.frame

Tony Ladson
  • 3,539
  • 1
  • 23
  • 30