John Eriksson
John Eriksson

Reputation: 13

making a table with multiple columns in r

I´m obviously a novice in writing R-code. I have tried multiple solutions to my problem from stackoverflow but I'm still stuck.

My dataset is carcinoid, patients with a small bowel cancer, with multiple variables.

i would like to know how different variables are distributed

carcinoid$met_any - with metastatic disease 1=yes, 2=no(computed variable)
carcinoid$liver_mets_y_n  - liver metastases 1=yes, 2=no
carcinoid$regional_lymph_nodes_y_n  - regional lymph nodes 1=yes, 2=no
peritoneal_carcinosis_y_n  - peritoneal carcinosis 1=yes, 2=no

i have tried this solution which is close to my wanted result

ddply(carcinoid, .(carcinoid$met_any), summarize,
      livermetastases=sum(carcinoid$liver_mets_y_n=="1"),
      regionalmets=sum(carcinoid$regional_lymph_nodes_y_n=="1"),
      pc=sum(carcinoid$peritoneal_carcinosis_y_n=="1"))

with the result being:

  carcinoid$met_any livermetastases regionalmets pc
1                 1              21           46  7
2                 2              21           46  7

Now, i expected the row with 2(=no metastases), to be empty. i would also like the rows in the column carcinoid$met_any to give the number of patients.

If someone could help me it would be very much appreciated! John

Edit

My dataset, although the column numbers are: 1, 43,28,31,33
1=yes2=no

case_nr          met_any     liver_mets_y_n   regional_lymph_nodes_y_n pc
1                   1               1                  1                2
2                   1               2                  1                2               
3                   2               2                  2                2
4                   1               2                  1                1               
5                   1               2                  1                1

desired output - I want to count the numbers of 1:s and 2:s, if it works, all 1:s should end up in the met_any=1 row

           nr   liver_mets        regional_lymph_nodes        pc
met_any=1  4         1                    4                    2
met_any=2  1         4                    1                    3

EDIT

Although i probably was very unclear in my question, with your help i could make the table i needed!

setDT(carcinoid)[,lapply(.SD,table),.SDcols=c(43,28,31,33,17)]

gives

     met_any lymph_nod liver_met     paraortal            extrahep
1:      50      46       21              6               15
2:     111     115      140             151              146

i am very grateful! @mtoto provided the solution John

Upvotes: 1

Views: 159

Answers (1)

mtoto
mtoto

Reputation: 24198

Based on your example data, this data.table approach works:

library(data.table)
setDT(df)[,lapply(.SD,table),.SDcols=c(2:5)]

# met_any liver_mets_y_n regional_lymph_nodes_y_n pc
# 1:       4              1                        4  2
# 2:       1              4                        1  3

Upvotes: 1

Related Questions