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

4
  • What is exp?... Commented Nov 27, 2017 at 9:32
  • @sotos edited thanks Commented Nov 27, 2017 at 9:36
  • You might do this df <- data.frame(test = NA). Please tell us your intended purpose of initializing an empty data.frame, maybe we can give better advice then. Commented Nov 27, 2017 at 9:43
  • You probably want dfNbMatchSurface$exp<- ""? Commented Nov 27, 2017 at 11:36

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.