0

i have the following dataframe stored inside a variable:

  project_name task_name
6              TPV

This dataframe is stored in variable called isPNfound.

If the task name is queried in the dataframe, i'm supposed to get back the project name. But when the project name is empty, the following code is suppose to catch that and alert:

if(isPNfound$project_name == "") {
    dataStatus <- paste("Task [", projectTaskName, "] has no Project assigned to it!!!")
    print(dataStatus)
    chkstatus <- 0
}

The above code works if there is only one taskname in the dataframe matching "TPV". But suppose there are multiple lines in the data frame, I need to run a loop on it. But im clueless on how to do that.

Sample content of dataframe:

  project_name task_name
6 Rice TPV
7 Beans TPV
8       TPV

I need to somehow run my code in a loop against this dataframe. Here's my attempt:

isPNfound <- dataframeContent
if (length(isPNfound) > 1) {
    for (eachtask in isPNfound){
        dataStatus <- paste("Task [", projectTaskName, "] has no    Project assigned to it!!!")
        print(dataStatus)
        chkstatus <- 0
    }
 }
2
  • 2
    Why loop? dataStatus <- ifelse( isPNfound$project_name == "", paste("Task [", isPNfound$TaskName, "] has no Project assigned to it!!!"), NA) Commented Sep 18, 2018 at 6:09
  • 1
    if(isPNfound$project_name == "") is identical with if(isPNfound$project_name[1] == ""). You should get a related warning: stackoverflow.com/questions/4367177/… Commented Sep 18, 2018 at 6:25

1 Answer 1

2

Something like this?

chkstatus <- isPNfound$project_name != ""
dataStatus <- ifelse(chkstatus,
              "OK",
              paste("Task [", isPNfound$task_name, "] has no Project assigned to it!!!")
)

chkstatus <- as.integer(chkstatus)

chkstatus
#[1] 1 1 0

dataStatus
#[1] "OK"                                           
#[2] "OK"                                           
#[3] "Task [ TPV ] has no Project assigned to it!!!"

DATA.

isPNfound <-
structure(list(project_name = c("Rice", "Beans", ""), 
task_name = c("TPV", 
"TPV", "TPV")), row.names = c("6", "7", "8"), class = "data.frame")
Sign up to request clarification or add additional context in comments.

1 Comment

@jogo Right. And coerce to integer as the OP wants only after ifelse. Will edit.

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.