how define a new column with respect of 4 columns?

elham

I want to make a new column loop as below: The first column is the index of the household. the second column is the index of person in that household . the third column is the index of trip of each day in each family during a day. the zoneOfHome is the zone of the home of that household. start_zone is a zone that a person start his/her trip from there and end_zone is the zone of a place that a person travel to there. last column is indicator when a person back home. A loop is a sequence of trips which starts from home and ends at home. I want a new column 'loop' which determine loop of each trip of household member.

tibble::tribble(
  ~Household, ~person, ~trip, ~ZoneOfHome, ~start_zone, ~end_zone,   ~purpose,
          1L,      1L,    1L,         22L,         22L,       13L,     0,
          1L,      1L,    2L,         22L,         13L,       22L,     1,
          1L,      1L,    3L,         22L,         22L,       34L,     0,
          1L,      1L,    4L,         22L,         34L,       22L,     1,
          1L,      2L,    1L,         22L,         22L,       13L,     0,
          1L,      2L,    2L,         22L,         13L,       22L,     1,
          2L,      1L,    1L,         15L,         15L,       15L,     0,
          2L,      1L,    2L,         15L,         15L,       15L,     1,
          2L,      1L,    3L,         15L,         15L,       45L,     0,
          2L,      1L,    4L,         15L,         45L,       15L,     1,
          3L,      1L,    1L,         17L,          6L,       17L,     1,
          3L,      1L,    2L,         17L,         17L,       10L,     0,
          3L,      1L,    3L,         17L,         10L,       17L,     1
  )

For each person a loop is start when start_zone=zone until indicator is 1.

Household   person    trip    ZoneOfHome    start_zone   end_zone       loop
   1           1        1      22              22           13            1
   1           1        2      22              13           22            1
   1           1        3      22              22           34            2
   1           1        4      22              34           22            2
   1           2        1      22              22           13            1
   1           2        2      22              13           22            1
   2           1        1      15              15           15            1
   2           1        2      15              15           15            1
   2           1        3      15              15           45            2
   2           1        4      15              45           15            2
   3           1        1      17              6            17            -
   3           1        2      17              17           10            1
   3           1        3      17              10           17            1
Uwe

The additional purpose column adds information which makes it much easier to identify loops than in the original question. If I understand correctly, purpose is 1 if the destination of a trip is the home of the person.

library(data.table)
setDT(DT)[, loop := cumsum(start_zone == ZoneOfHome & purpose != 1), by = .(Household, person)][]
    Household person trip ZoneOfHome start_zone end_zone purpose loop
 1:         1      1    1         22         22       13       0    1
 2:         1      1    2         22         13       22       1    1
 3:         1      1    3         22         22       34       0    2
 4:         1      1    4         22         34       22       1    2
 5:         1      2    1         22         22       13       0    1
 6:         1      2    2         22         13       22       1    1
 7:         2      1    1         15         15       15       0    1
 8:         2      1    2         15         15       15       1    1
 9:         2      1    3         15         15       45       0    2
10:         2      1    4         15         45       15       1    2
11:         3      1    1         17          6       17       1    0
12:         3      1    2         17         17       10       0    1
13:         3      1    3         17         10       17       1    1

Explanation

The OP has defined

A loop is a sequence of trips which starts from home and ends at home.

So, we need to identify which trips start at home. This information is not explicitely given but can be derived from the condition start_zone == ZoneOfHome & purpose != 1, i.e., the trip starts in the home zone but is not heading home. The expression cumsum(start_zone == ZoneOfHome & purpose != 1) is advanced by one whenever a new loop starts (for each person in a household).

Improved version:

Note that we do not explicitely test for the end of trip, i.e., purpose == 1. It is assumed that a trip which starts in the home zone and is not heading home starts at home and that the previous trip has ended at home. This might not be true for more complex trip patterns.

So, it might be safer to include a test that the previous trip of a person has ended at home indeed:

setDT(DT)[, loop := cumsum(start_zone == ZoneOfHome & shift(purpose, fill = 1) == 1), 
          by = .(Household, person)][]

The result is the same as above for the given sample dataset.

So, a new loop starts from home if the starting point is in the home zone and the previous trip has ended at home.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to process column names and create new columns

How to find out sum of a column with respect to another columns maximum and minimum

How to extract new substring as column columns

how to get values in new column based on other column group by columns

How to compare columns and add a new column

how to change value of a column with respect of 3 groups

how define a new column with 2 group and 1 column

how to define an indicator with respect of 4 columns?

How to slice multiple columns and sort the values with respect to another column in pandas

How to transform a column value into new columns in python?

pivot one column in pandas dataframe and create 4 new columns

Transpose dataframe with respect to a column without duplicate columns

How update column with 4 columns in sql

RDBMS Splitting a column into many columns with respect to a field

How to pivot rows to columns and column data under the new columns?

How to define a new column in a stored procedure query

iccube : in a google column chart, how to define gap between columns?

Pandas: Define type of new column

how to add value of different column with respect of a column

how I can add 1 to a values of a column whit respect of 2 columns?

how to add 2 column with respect of group

define a new column

Adding new column and define the results

How to convert a JSON column in a tibble to new columns

How fill NA with respect of another column?

How to concatenate the result of columns to a new column

how to divide all the values of columns with respect of a row?

Unpivot String Column into 4 new Columns SQL / Snowflake

How to define a new column in a data frame based on row-wise condition of different sets of columns

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive