0

I created a data frame in r called "test". When I am trying to add a column it throws me an error. Below is the code i use to create a column:

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

Error I get:

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 have just started using R. Would greatly appreciate your help.

1 Answer 1

2

I can reproduce this with the iris dataset 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 no column sex in test. (Keep in mind that variable names in R are case-sensitive.)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Roland, Thank you for your quick response. I got this and also fixed it. Will be more aware regarding this going forward....:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.