Modifying the value for a particular variable based on a condition

Lonewolf

I am trying to get the output below, but I do not know how to get it in R. This is the data I have:

      ID    PERIOD     rating
       8      0         3
       8      1         3
       8      2         2
       8      3         F
       8      4         3
       8      5         F
       8      6         1
       9      0         2
       9      1         2
       9      2         1

Below is the output I want.

     ID    PERIOD     rating
      8      0         3
      8      1         3
      8      2         2
      8      3         F
      8      4         F
      8      5         F
      8      6         F
      9      0         2
      9      1         2
      9      2         1 

As you can see, As soon as the rating hit "F" for a particular ID then the rating should remain "F" for that ID. I do not know how to go about coding. Any help will be appreciated.

sindri_baldur

Using data.table:

setDT(data)

data[, rating := ifelse(cumsum(rating == "F") >= 1, "F", rating), by = ID]

data
    ID PERIOD rating
 1:  8      0      3
 2:  8      1      3
 3:  8      2      2
 4:  8      3      F
 5:  8      4      F
 6:  8      5      F
 7:  8      6      F
 8:  9      0      2
 9:  9      1      2
10:  9      2      1

Where

data <- data.frame(
  ID = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L), 
  PERIOD = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 0L, 1L, 2L), 
  rating = c("3", "3", "2", "F", "3", "F", "1", "2", "2", "1"),
  stringsAsFactors = FALSE
)

EDIT

Long story, but this could be made more concise:

data[, rating := ifelse(cumsum(rating == "F"), "F", rating), by = ID]

EDIT 2

As Ronak's suggests you could do something like the following using ave() that comes with base R:

data$rating <- 
  ifelse(ave(data$rating == "F", data$ID, FUN = cumsum), "F", data$rating) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Modifying value of a particular tag in xml

From Dev

Assign value to variable based on condition (value of other variable) in PLSQL

From Dev

Modifying the value of macro variable in SAS

From Dev

In Robot Framwork how to set value of variable based on some condition

From Dev

How to remove rows based on a particular condition

From Dev

Condition based variable in XSLT

From Dev

Modifying elasticsearch score based on nested field value

From Dev

condition is not assigning value to the variable

From Dev

Setting variable based on a folder at a particular path

From Dev

Condition based on environment variable existance

From Dev

Sort multidimensional array based on the value of a particular index

From Dev

Instantiate particular class based on object value

From Dev

Sort multidimensional array based on the value of a particular index

From Dev

Modifying row value based on preceding row value in pandas/python

From Java

Replace characters with particular format with a variable value in python

From Dev

Assembly - How to see the value in a particular variable with gdb

From Dev

Take particular value from the variable having html

From Dev

Assembly - How to see the value in a particular variable with gdb

From Dev

How to get records based on particular Column when multiple condition meets

From Dev

How to delete a particular line in xml file based on the condition using python?

From Dev

Select particular record from another table based on condition

From Dev

Get array of particular property of model obj based on condition Swift

From Dev

Assign a value based on if condition in Scala

From Dev

Replace value in column based on a condition

From Dev

Nokogiri condition based on attribute value

From Dev

Match a value in VBA based on a condition

From Dev

Assign value based on a condition in javascript

From Dev

Increment value in the list based on condition

From Java

Set value to a string variable, from an object based on the condition using Java Streams

Related Related

  1. 1

    Modifying value of a particular tag in xml

  2. 2

    Assign value to variable based on condition (value of other variable) in PLSQL

  3. 3

    Modifying the value of macro variable in SAS

  4. 4

    In Robot Framwork how to set value of variable based on some condition

  5. 5

    How to remove rows based on a particular condition

  6. 6

    Condition based variable in XSLT

  7. 7

    Modifying elasticsearch score based on nested field value

  8. 8

    condition is not assigning value to the variable

  9. 9

    Setting variable based on a folder at a particular path

  10. 10

    Condition based on environment variable existance

  11. 11

    Sort multidimensional array based on the value of a particular index

  12. 12

    Instantiate particular class based on object value

  13. 13

    Sort multidimensional array based on the value of a particular index

  14. 14

    Modifying row value based on preceding row value in pandas/python

  15. 15

    Replace characters with particular format with a variable value in python

  16. 16

    Assembly - How to see the value in a particular variable with gdb

  17. 17

    Take particular value from the variable having html

  18. 18

    Assembly - How to see the value in a particular variable with gdb

  19. 19

    How to get records based on particular Column when multiple condition meets

  20. 20

    How to delete a particular line in xml file based on the condition using python?

  21. 21

    Select particular record from another table based on condition

  22. 22

    Get array of particular property of model obj based on condition Swift

  23. 23

    Assign a value based on if condition in Scala

  24. 24

    Replace value in column based on a condition

  25. 25

    Nokogiri condition based on attribute value

  26. 26

    Match a value in VBA based on a condition

  27. 27

    Assign value based on a condition in javascript

  28. 28

    Increment value in the list based on condition

  29. 29

    Set value to a string variable, from an object based on the condition using Java Streams

HotTag

Archive