Assigning values in first rows of groups in a data.table

MDS

I'd like to assign only those values in the first row of a group in a data.table.

For example (simplified): my data.table is DT with the following content

x v  
1 1  
2 2  
2 3  
3 4  
3 5  
3 6 

The key of DT is x.
I want to address every first line of a group.

This is working fine:DT[, .SD[1], by=x]

x v  
1 1  
2 2  
3 4 

Now, I want to assign only those values of v to 0.

But none of this is working:

DT[, .SD[1], by=x]$v <- 0  
DT[, .SD[1], by=x]$v := 0  
DT[, .SD[1], by=x, v:=0]

I searched the R-help from the package and any links provided but I just can't get it work.
I found notes there saying this would not work but no examples/solutions that helped me out.

I'd be very glad for any suggestions.

(I like this package very much and I don't wanna go back to a data.frame... where I got this working)

edit:

I'd like to have a result like this:

x v  
1 0  
2 0  
2 3  
3 0  
3 5  
3 6  

This is not working:

DT[, .SD[1], by=x] <- DT[, .SD[1], by=x][, v:=0]
Rich Scriven

With a little help from Roland's solution, it looks like you could do the following. It simply concatenates zero with all the other grouped values of v except the first.

DT[, v := c(0L, v[-1]), by = x]   ## must have the "L" after 0, as 0L

which results in

DT
#    x v
# 1: 1 0
# 2: 2 0
# 3: 2 3
# 4: 3 0
# 5: 3 5
# 6: 3 6

Note: the middle section j of code could also be v := c(integer(1), v[-1])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

assigning groups of ID values to a number of rows in R

From Dev

assigning a subset of data.table rows and columns by join

From Dev

assigning a subset of data.table rows and columns by join

From Dev

R: Efficient way of assigning values to data.table with multiple conditions

From Dev

select most recent rows for duplicate groups with data.table in R

From Dev

Assigning rank of values within groups with NAs

From Java

data.table linearly interpolating NA values without groups

From Dev

jQuery assigning values to dropdown in a table

From Dev

Fill null values for groups of rows

From Dev

Fill null values for groups of rows

From Dev

Memcpy - assigning values to block of data

From Dev

SAS assigning binary values if variable matches in first instance for 'n' times for unsorted data

From Dev

Assigning partner values for paired rows in SPSS

From Dev

R how to get data column to rows of first and second values

From Dev

Keep first and last rows of repetitive columns values of a panda data frame

From Dev

SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

From Dev

SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

From Dev

Delete all data rows from an Excel table (apart from the first)

From Dev

remove rows by reference to column values in data.table r

From Dev

aggregate data.table to rows of intervals of original values

From Dev

Change data.table values in one column for multiple rows

From Dev

Compare consecutive rows in data.table and replace row values

From Dev

Modify values in random rows of a data.table by category

From Dev

Adding rows to a data.table according to column values

From Dev

updating "multiple" columns of "selected" rows of a data table with duplicate key values

From Dev

Adding rows to a data.table according to column values

From Dev

aggregate data.table to rows of intervals of original values

From Dev

Transfer only rows that have different values as existing table data

From Dev

Assigning value to an observation from table of values

Related Related

  1. 1

    assigning groups of ID values to a number of rows in R

  2. 2

    assigning a subset of data.table rows and columns by join

  3. 3

    assigning a subset of data.table rows and columns by join

  4. 4

    R: Efficient way of assigning values to data.table with multiple conditions

  5. 5

    select most recent rows for duplicate groups with data.table in R

  6. 6

    Assigning rank of values within groups with NAs

  7. 7

    data.table linearly interpolating NA values without groups

  8. 8

    jQuery assigning values to dropdown in a table

  9. 9

    Fill null values for groups of rows

  10. 10

    Fill null values for groups of rows

  11. 11

    Memcpy - assigning values to block of data

  12. 12

    SAS assigning binary values if variable matches in first instance for 'n' times for unsorted data

  13. 13

    Assigning partner values for paired rows in SPSS

  14. 14

    R how to get data column to rows of first and second values

  15. 15

    Keep first and last rows of repetitive columns values of a panda data frame

  16. 16

    SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

  17. 17

    SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

  18. 18

    Delete all data rows from an Excel table (apart from the first)

  19. 19

    remove rows by reference to column values in data.table r

  20. 20

    aggregate data.table to rows of intervals of original values

  21. 21

    Change data.table values in one column for multiple rows

  22. 22

    Compare consecutive rows in data.table and replace row values

  23. 23

    Modify values in random rows of a data.table by category

  24. 24

    Adding rows to a data.table according to column values

  25. 25

    updating "multiple" columns of "selected" rows of a data table with duplicate key values

  26. 26

    Adding rows to a data.table according to column values

  27. 27

    aggregate data.table to rows of intervals of original values

  28. 28

    Transfer only rows that have different values as existing table data

  29. 29

    Assigning value to an observation from table of values

HotTag

Archive