change data in column with the previous information in another column

Cina

I have a dataset with three columns. The column user has two actions including action1 and action2. action2 only contains information if action1 column has A data. I want to concatenate P data in action1 with the previous data in action2. For example, if action2 has ac, and the next row has P in action1, I want to change P into Pac, and will continue (all P turn into Pac) until action2 change. Take note that this process should repeat for each user.

df<-read.table(text="
user   action1    action2 
1        A          a
1        B          NA
1        P          NA
1        P          NA
1        A          ac
1        P          NA
2        B          NA
2        P          NA
2        A          aa
2        P          NA
2        AB         aa",header=T)

result: (I highlighted those rows that infected)
user   action1    action2 
1        A          a
1        B          NA
1        Pa         NA <-
1        Pa         NA <-
1        A          ac
1        Pac        NA <-
2        B          NA
2        P          NA
2        A          aa
2        Paa        NA <-
2        AB         NA

Thank you

Sathish
library('data.table')
library('zoo')
# Using zoo::na.locf(), fill NA with the previous value and group by user. Also `na.locf` will not remove NA.
setDT(df)[, V3 := na.locf(action2, na.rm = FALSE), by = .(user)]     
# combine action1 with V3 column if action1 is equal to 'P' and it is not NA.
df[action1 == 'P' & !(is.na(V3)), action1 := paste0(action1, V3)] 
df[, V3 := NULL] # remove V3 column
df
#    user action1 action2
# 1:    1       A       a
# 2:    1       B      NA
# 3:    1      Pa      NA
# 4:    1      Pa      NA
# 5:    1       A      ac
# 6:    1     Pac      NA
# 7:    2       B      NA
# 8:    2       P      NA
# 9:    2       A      aa
# 10:    2     Paa      NA
# 11:    2      AB      aa

Data:

df<-read.table(text="
user   action1    action2 
               1        A          a
               1        B          NA
               1        P          NA
               1        P          NA
               1        A          ac
               1        P          NA
               2        B          NA
               2        P          NA
               2        A          aa
               2        P          NA
               2        AB         aa",header=T, stringsAsFactors = FALSE)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Copy data from one column to another column

분류에서Dev

How to copy one column data into another column in oracle

분류에서Dev

Conditionally choose data from different columns based of data in another column

분류에서Dev

perform operation on column of data frame based on condition given to column in another data frame in pandas

분류에서Dev

PARTITION BY multiple column while inserting data from another table

분류에서Dev

Moving some data from one column of MySQL table to another

분류에서Dev

How do I change decimal place in stored column data?

분류에서Dev

R - Join on data.table, selecting a different column based on value of another column in row

분류에서Dev

How to change cell value down one column into another but change the row value?

분류에서Dev

Change column to unique

분류에서Dev

Compare character in column with string in another column

분류에서Dev

Excel - Group rows by column then sort by another column

분류에서Dev

Aggregate previous rows of into lists, depending on separate column

분류에서Dev

SUMIF column in column's row, is the same as another column

분류에서Dev

Search for column values in another column and assign a value from the next column from the row found to another column

분류에서Dev

Excel - sum values based on a column that match another column in another table

분류에서Dev

Comparing one column of cells to another

분류에서Dev

Replace to another column if exits in MySql

분류에서Dev

Data Grid Column Labeling‏

분류에서Dev

Get data except for data in a column

분류에서Dev

mysql order data from table by column using relations from another table

분류에서Dev

Unable to copy data from one Excel sheet to another when not copying to column A

분류에서Dev

Unable to copy data from one Excel sheet to another when not copying to column A

분류에서Dev

Update Table With An Empty Column With Data From Another Table using a shared id from a third table

분류에서Dev

How to sum multiple cell of a same column based on another column and display in another column?

분류에서Dev

Change column data type in MySQL without losing other metadata (DEFAULT, NOTNULL...)

분류에서Dev

SQL Server - Select column to update based on another column value

분류에서Dev

Select a column based on another column's value with Pandas

분류에서Dev

Create a new column value based on another column content from a list

Related 관련 기사

  1. 1

    Copy data from one column to another column

  2. 2

    How to copy one column data into another column in oracle

  3. 3

    Conditionally choose data from different columns based of data in another column

  4. 4

    perform operation on column of data frame based on condition given to column in another data frame in pandas

  5. 5

    PARTITION BY multiple column while inserting data from another table

  6. 6

    Moving some data from one column of MySQL table to another

  7. 7

    How do I change decimal place in stored column data?

  8. 8

    R - Join on data.table, selecting a different column based on value of another column in row

  9. 9

    How to change cell value down one column into another but change the row value?

  10. 10

    Change column to unique

  11. 11

    Compare character in column with string in another column

  12. 12

    Excel - Group rows by column then sort by another column

  13. 13

    Aggregate previous rows of into lists, depending on separate column

  14. 14

    SUMIF column in column's row, is the same as another column

  15. 15

    Search for column values in another column and assign a value from the next column from the row found to another column

  16. 16

    Excel - sum values based on a column that match another column in another table

  17. 17

    Comparing one column of cells to another

  18. 18

    Replace to another column if exits in MySql

  19. 19

    Data Grid Column Labeling‏

  20. 20

    Get data except for data in a column

  21. 21

    mysql order data from table by column using relations from another table

  22. 22

    Unable to copy data from one Excel sheet to another when not copying to column A

  23. 23

    Unable to copy data from one Excel sheet to another when not copying to column A

  24. 24

    Update Table With An Empty Column With Data From Another Table using a shared id from a third table

  25. 25

    How to sum multiple cell of a same column based on another column and display in another column?

  26. 26

    Change column data type in MySQL without losing other metadata (DEFAULT, NOTNULL...)

  27. 27

    SQL Server - Select column to update based on another column value

  28. 28

    Select a column based on another column's value with Pandas

  29. 29

    Create a new column value based on another column content from a list

뜨겁다태그

보관