1

I have a Blood pressure Dataframe with the below data.

   Name Age M.F Blood.Pressure
1     A  40   M            110
2     B  55   F            112
3     C  51   M            144
4     D  14   M            134
5     E  48   M             90
6     F  78   M             85
7     G  21   F            135
8     H  59   M            150
9     I  32   F             98
.....

I need to check B.P. of each record and based on the values, I have to create a new column with the strings ("Low","Normal","High") corresponding to B.P. I am using the below code to do that.

excel <- read.csv("SampleCSVFile.csv", header=T)
df1 <- data.frame(excel)
df1

for(i in length(df2$Blood.Pressure))
{
  if (df1$Blood.Pressure > 80 & df1$Blood.Pressure < 120)
  {
      df1$is.cond <- c("Low")
  }

  else if(df1$Blood.Pressure > 120 & df1$Blood.Pressure < 140)
  {
      df1$is.cond <- c("Normal")
  }
  else if(df1$Blood.Pressure > 140)
  {
      df1$is.cond <- c("High")
  }
}
df1

But this is the output that I am getting.

   Name Age M.F Blood.Pressure is.cond
1     A  40   M            110     Low
2     B  55   F            112     Low
3     C  51   M            144     Low
4     D  14   M            134     Low
5     E  48   M             90     Low
6     F  78   M             85     Low
7     G  21   F            135     Low
8     H  59   M            150     Low
9     I  32   F             98     Low
10    J  63   M            150     Low

It is not checking each row, it just checks the first Blood pressure value, and based on that it assigns the string to all the rows. can you please help ?

3
  • That's because you're not even using looping variable 'i' anywhere. And please read the policy of how to ask good questions here : stackoverflow.com/questions/ask/advice? Commented Jun 29, 2016 at 5:20
  • 2
    What does python have to do with this? Commented Jun 29, 2016 at 5:51
  • Or Excel? Or CSV? Don't use tags just because they might be suggested. Commented Jun 29, 2016 at 5:54

3 Answers 3

4

You don't need a loop. This is a vectorized way to do things:

# initialize column
df1$is.cond = NA_character_

df1$is.cond[df1$Blood.Pressure > 80 & df1$Blood.Pressure < 120] = "Low"
df1$is.cond[df1$Blood.Pressure > 120 & df1$Blood.Pressure < 140] = "Normal"
df1$is.cond[df1$Blood.Pressure > 140] = "High"

However, for binning data like this the built-in function cut does all the work for us:

df1$is.cond.cut = cut(df1$Blood.Pressure,
                      breaks = c(-Inf, 80, 120, 140, Inf),
                      labels = c("unknown", "Low", "Normal", "High"))

In your method (and in my first example), because you use < rather than <=, no value will be assigned to blood pressures exactly on the threshholds. You might want to change that. cut defaults to intervals that are "closed on the right", meaning that a value of 120 would be considered Low. You can change this behavior by giving it the argument right = F.

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

1 Comment

Thanks a lot for this answer!
1

How about a cleaner solution :

df1$is.cond <- ifelse(df1$Blood.Pressure > 80 & df1$Blood.Pressure < 120, "Low",
          ifelse(df1$Blood.Pressure > 120 & df1$Blood.Pressure < 140, "Normal", 
             ifelse(df1$Blood.Pressure > 140, "High" , "None")))

1 Comment

I got this idea as soon as I posted this question. Anyway thanks for your suggestion!.
1

See if that's what you needed .

df1 <- read.csv("file_to_read.csv",T,"~")


for(i in 1:nrow(df1))
{
  if (df1$Blood.Pressure[i] > 80 & df1$Blood.Pressure[i] < 120)
  {
    df1$is.cond[i] <- c("Low")
  }

  else if(df1$Blood.Pressure[i] > 120 & df1$Blood.Pressure[i] < 140)
  {
    df1$is.cond[i] <- c("Normal")
  }
  else if(df1$Blood.Pressure[i] > 140)
  {
    df1$is.cond[i] <- c("High")
  }
}

> df1
  Name Age M.F Blood.Pressure is.cond
1    A  40   M            110     Low
2    B  55   F            112     Low
3    C  51   M            144    High
4    D  14   M            134  Normal
5    E  48   M             90     Low
6    F  78   M             85     Low
7    G  21   F            135  Normal
8    H  59   M            150    High
9    I  32   F             98     Low

1 Comment

Thanks a lot for your suggestion. This is what I wanted.

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.