0

the nested if statement in this code is working very fine

x <- 4
if(x == 4){
  t <- x + 1
  if(t == 5){
   t + 2
 }
}
[1] 7

but when i try the same code on data frame there is no results.

mydata_one <- data.frame(words = c("hello", "everyone"))
mydata_two <- data.frame(words = c("my", "name"))
if(length(mydata_one$word) == 2){
  big_data <- rbind(mydata_one, mydata_two)
  if(length(big_data) > 3){
    big_data[1:3, 1]
   }
}

The expected value i am looking for:

[1] hello    everyone my
1
  • a data frame is just a special list. you haven't changed the # of columns so your (devoid of useful whitespace) if (length(big_data) > 3) { is still 1. Commented Oct 7, 2017 at 11:27

1 Answer 1

1

Length will not work with data frame. Use nrow instead to get number of records.

mydata_one <- data.frame(words = c("hello", "everyone"))
mydata_two <- data.frame(words = c("my", "name"))
if(length(mydata_one$word) == 2){
  big_data <- rbind(mydata_one, mydata_two)
  if(nrow(big_data) > 3){
    big_data[1:3, 1]
   }
}

Output: [1] hello everyone my
Levels: everyone hello my name

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

Comments

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.