Creating a data frame in R

I have created a dataframe in r called "test". When I try to add a column it throws me an error. Below is the code I am using to create the column:

test$survived[test$sex == "male"] <- 1

      

Mistake:

Error in $<-.data.frame(*tmp*, "survived", value = numeric(0)) : 
replacement has 0 rows, data has 418

      

I also tried using ifelse, but it gives me the same error message. I just started using R. Thanks a lot for your help.

0


source to share


1 answer


I can reproduce this using a dataset iris

like this:

iris$newcol[iris$blub == "blub"] <- 1
#Error in `$<-.data.frame`(`*tmp*`, "newcol", value = numeric(0)) : 
#  replacement has 0 rows, data has 150

      



There is test

no column sex

. (Note that variable names in R are case sensitive.)

+2


source







All Articles