1

Here is my code :

dfNbMatchSurface = data.frame()
print(dfNbMatchSurface)
dfNbMatchSurface$test <- "exp"
write.csv(dfNbMatchSurface, file = "NbMatchSurface.csv")

And i want to create an empty dataframe with a new test column that is empty and call "exp"

How to do that ?

I've also tried this :

dfNbMatchSurface = data.frame()
print(dfNbMatchSurface)
dfNbMatchSurface$test <- NA 
write.csv(dfNbMatchSurface, file = "NbMatchSurface.csv")

I have thos error :

Error in `$<-.data.frame`(`*tmp*`, test, value = "exp") : 
  replacement has 1 row, data has 0

Regards and thanks

Bussiere
  • 500
  • 13
  • 60
  • 119

1 Answers1

5

You can use character() to assign a class to the variable without including values (other classes also work, like factor(), integer() or numeric()).

df <- data.frame()
> df
data frame with 0 columns and 0 rows

df$var1 <- character()
> df
[1] var1
<0 rows> (or row.names with length 0)
LAP
  • 6,605
  • 2
  • 15
  • 28