concatenate column name and column values

Tom Martens

i have the following

library(data.table)

anid <- c(1,2,3,4)
agroup <- c("m", "m", "f", "f")
anothergroup <- c("a","c", NA, "c")
avalue <- c(11,  6, 17, 3)
mygoal <- c("agroup:m_anothergroup:a","agroup:m_anothergroup:c","agroup:f_anothergroup:NA","agroup:f_anothergroup:c")

unfortunately i missed this line in my example

dt <- data.table(anid, agroup, anothergroup, avalue)

Basically I want to create the values of the column mygoal using a function, but unfortunately im stuck. I want to create the column mygoal using something similar like this

dt[, mygoal:= lapply(...)]

The number of columns that go into the function can vary, but nevertheless I know them but it has to be possible to provide the columns to the function as character vector. In the above example the columns "agroup" and "anothergroup" are used to create values for the column "mygoal".

Once again, any hint is appreciated

Tom

Roland
cols <- c("agroup", "anothergroup")
DT[, mygoal := do.call(paste, 
                       c(lapply(cols, function(x) paste(x, get(x), sep=":")),              
                         sep="_"))]
#   anid agroup anothergroup avalue                   mygoal
#1:    1      m            a     11  agroup:m_anothergroup:a
#2:    2      m            c      6  agroup:m_anothergroup:c
#3:    3      f           NA     17 agroup:f_anothergroup:NA
#4:    4      f            c      3  agroup:f_anothergroup:c

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Concatenate column values

From Dev

Concatenate mysql column values in php

From Dev

Concatenate column values against another column group

From Dev

Concatenate column values in Pandas DataFrame with "NaN" values

From Dev

Concatenate characters of string values to the same column values

From Dev

Concatenate column values in Pandas DataFrame with "NaN" values

From Dev

Python Pandas: Join on unique column values and concatenate

From Dev

SQL Server concatenate a column's values on subquery

From Dev

Concatenate values which don't exist in the column

From Dev

Concatenate Column Values Row by Row Dynamically

From Dev

Pivot and concatenate values from column in SQL Server

From Dev

Python Pandas: Join on unique column values and concatenate

From Dev

Concatenate values from column in related table into a string

From Dev

Concatenate Column Values Row by Row Dynamically

From Dev

SQL Server concatenate a column's values on subquery

From Dev

Best way to concatenate column values in aggregate

From Dev

how to concatenate two column values and store in a another column mysql php

From Dev

Concatenate a set of column values based on another column in Pandas

From Dev

how to concatenate two column values and store in a another column mysql php

From Dev

Group column from CSV file and concatenate values of another column

From Dev

How to swap column values to be column name

From Dev

Paste multiple column values with column name

From Dev

Return column name and distinct values

From Dev

Give column name to values rows

From Dev

Find multiple values, concatenate cooresponding values in other column, write to cell

From Dev

Invalid column name ' ' on concatenate columns with Laravel 4 Eloquent?

From Dev

Concatenate two numerical values to make a new column using pandas?

From Dev

Trying to concatenate LINQ column values, Not sure where the LINQ error is?

From Dev

SQL Query to Select specific column values and Concatenate them

Related Related

  1. 1

    Concatenate column values

  2. 2

    Concatenate mysql column values in php

  3. 3

    Concatenate column values against another column group

  4. 4

    Concatenate column values in Pandas DataFrame with "NaN" values

  5. 5

    Concatenate characters of string values to the same column values

  6. 6

    Concatenate column values in Pandas DataFrame with "NaN" values

  7. 7

    Python Pandas: Join on unique column values and concatenate

  8. 8

    SQL Server concatenate a column's values on subquery

  9. 9

    Concatenate values which don't exist in the column

  10. 10

    Concatenate Column Values Row by Row Dynamically

  11. 11

    Pivot and concatenate values from column in SQL Server

  12. 12

    Python Pandas: Join on unique column values and concatenate

  13. 13

    Concatenate values from column in related table into a string

  14. 14

    Concatenate Column Values Row by Row Dynamically

  15. 15

    SQL Server concatenate a column's values on subquery

  16. 16

    Best way to concatenate column values in aggregate

  17. 17

    how to concatenate two column values and store in a another column mysql php

  18. 18

    Concatenate a set of column values based on another column in Pandas

  19. 19

    how to concatenate two column values and store in a another column mysql php

  20. 20

    Group column from CSV file and concatenate values of another column

  21. 21

    How to swap column values to be column name

  22. 22

    Paste multiple column values with column name

  23. 23

    Return column name and distinct values

  24. 24

    Give column name to values rows

  25. 25

    Find multiple values, concatenate cooresponding values in other column, write to cell

  26. 26

    Invalid column name ' ' on concatenate columns with Laravel 4 Eloquent?

  27. 27

    Concatenate two numerical values to make a new column using pandas?

  28. 28

    Trying to concatenate LINQ column values, Not sure where the LINQ error is?

  29. 29

    SQL Query to Select specific column values and Concatenate them

HotTag

Archive