Question

I have this line of code that i am using in order to create the table below.

DtTemp <- Dtable[,list("temperature"=paste(temperature,collapse=", ")),by=station.id]

table:

   station.id                  temperature
1:         S1     18, 20.5, 18, 18.6, 21.5
2:         S2 20.1, 18.3, 16.8, 17.5, 16.4
3:         S3   11, 19.1, 18.9, 17.8, 17.6
4:         S4     18, 15.5, 15, 14.9, 15.8

In the table above, temperature values are grouped according to station.id.

I am looking for to create other similar tables, automatically, for every other column in my Dtable dataset. That being said it is obvious that i have more than one station.id values in my original dataset Dtable.

Assuming that a for loop might do it, or something like foreach or iter how should i use them to implement it.?

Is there a way to achieve this, giving at each temperature column, as the example above, the relevant Dtable column name, for which the table is created.?

Appreciate any help.

Was it helpful?

Solution

Yes, you have access to the .SD variable which represents all columns other than the ones involved in grouping, so

Dtable[,lapply(.SD, paste, collapse=", "), by=station.id]

should do the trick

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top