简体   繁体   中英

How to export summary from R to Excel Spreadsheet

I am trying to export my summary output from R into an Excel Spreadsheet. I am trying to filter my data down to what I need the summary from and then am trying to export this, ie dataframe -> filter 1 (called x) -> filter 2 (called y). This means I essentially need the summary from y exported.

df$group
x= filter(df,Group == "Intervention")
y= filter (x, Visit == "1")
View(x)
View(y)

summary(y)

This is a variation from many that I have tried and doesn't work:

install.packages("writexl")
library("writexl")

write_xlsx(as.data.frame(summary$y),"Summary_Intervention-V1.xlsx")

Thanks in advance!

Sample using dput(head(df)):

structure(list(Pseudonym = c(1L, 2L, 4L, 5L, 6L, 7L), Group = c("Intervention", 
"Intervention", "Intervention", "Intervention", "Intervention", 
"Intervention"), Visit = c(1L, 1L, 1L, 1L, 1L, 1L), Sex = c("M", 
"M", "M", "M", "M", "M"), Weight..kg. = c(90.2, 109.3, 78, 70.7, 
89.1, 91.2), Height..cm. = c(186, 192, 173, 182, 185, 175), Pulse.Rate..bpm. = c(76L, 
72L, 60L, 64L, 79L, 74L), Systolic.Blood.Pressure = c(150L, 140L, 
115L, 100L, 121L, 112L), Diastolic.Blood.Pressure = c(80L, 80L, 
69L, 59L, 81L, 98L), Respiration.Rate = c(20L, 16L, 13L, 16L, 
13L, 18L), HS.cTnT..ng.l. = c("18", "5", "78", "6", "12", "10"
), Myoglobin..ug.l. = c(NA, "31", "173", "21", "25", "63"), NT.proBNP..ng.l. = c(88L, 
55L, 60L, 14L, 282L, 256L), CK..U.l. = c(53L, 137L, 115L, 72L, 
59L, 188L), CK.MB = c(12.9, 14.5, 69, 15.6, 19.2, 28.2), CK.MB.... = c(25L, 
11L, 13L, 22L, 32L, 15L), CRP..mg.l. = c("5.3", "3.6", "1.2", 
"4.5", "1.1", "0.8"), Hemoglobin..g.dl. = c(15.6, 15.1, 14.4, 
14.1, 17.3, 16.2), Hematocrit..l.l. = c(0.437, 0.465, 0.441, 
0.427, 0.53, 0.459), Erythrocytes...pl. = c(4.9, 5.5, 5, 5.6, 
5.8, 4.9), Leucocytes...nl. = c(7.13, 6.75, 5.95, 8.36, 7.41, 
8.83), Thromobocytes...nl. = c(298L, 240L, 319L, 148L, 286L, 
184L), Mean.corpuscular.hemoglobin..pg. = c(32.2, 27.2, 29, 25.2, 
30.1, 33.3), Mean.corpuscular.hemoglobin.concentration..g.dl. = c(35.7, 
32.5, 32.7, 33, 32.6, 35.3), Mean.Corpuscular.Volume..fl. = c(90, 
85, 89, 76, 92, 94), Mean.platelet.volume..fl. = c(10, 10.8, 
12.2, 10.8, 10.9, 12.3), Red.cell.distribution.width..RDW.CV..... = c(12.5, 

13, 12.3, 12.5, 13.7, 12.3), Neutrophils.absolute...nl. = c(3.85, 
4.04, 3.74, 5.51, 4.65, 5.16), Neutrophils.... = c(54, 59.9, 
62.9, 65.9, 62.8, 58.6), Immature...banded.granuclocytes..Neutrophils....nl. = c(0.06, 
0.03, 0.02, 0.04, 0.04, 0.03), Immature...banded.granulocytes..Neutrophils..... = c(0.8, 
0.4, 0.3, 0.5, 0.5, 0.3), Reticulocytes...nl. = c(NA, 127.8, 
56.5, 37, 74.8, 89.9), Eosinophils.... = c(1.7, 2.8, 2.2, 0.7, 
1.2, 2), Eosinophils...nl. = c(0.12, 0.19, 0.13, 0.06, 0.09, 
0.18), Basophils.... = c(0.4, 0.6, 0.3, 0.4, 0.7, 1.1), Basophils...nl. = c(0.03, 
0.04, 0.02, 0.03, 0.05, 0.1), Lymphocytes...nl. = c(2.5, 1.73, 
1.54, 2.14, 1.91, 2.72), Lymphocytes.... = c(35.1, 25.6, 25.9, 
25.6, 25.8, 30.8), Monocytes.... = c(8, 10.7, 8.4, 6.9, 9, 7.2
), Monocytes...nl. = c(0.57, 0.72, 0.5, 0.58, 0.67, 0.64), Sodium..mmol.l. = c(137L, 
142L, 143L, 142L, 136L, 142L), Potassium..mmol.l. = c(3.3, 4.4, 
4.2, 4.7, 4.4, 4.4), Iron..umol.L. = c(11.9, 17.6, 18.4, 8.1, 
14.6, 25.2), Ferritin..ug.l. = c(54.4, 244.4, 100.5, 177.9, 137.6, 
535.9), Transferrin..g.l. = c(2.72, 2.43, 2.8, 2.54, 2.88, 2.14
), Transferrin.Saturation.... = c(17.4, 28.8, 26.2, 12.7, 20.2, 
46.9), Total.Calcium..mmol.l. = c(NA, 2.37, 2.39, 2.46, 2.33, 
2.22), Calcium.corrected..mmol.l. = c(NA, 2.24, 2.2, 2.39, 2.23, 
2.14), Chloride..mmol.l. = c(NA, 106L, 102L, 101L, 102L, 106L
), Magnesium..mmol.l. = c(NA, NA, 0.86, 0.88, 0.86, 0.78), Phosphate..mmol.l. = c(NA, 

NA, 1.28, 1.27, 1.07, 0.9), Glucose..mg.dl. = c(126L, 82L, 107L, 
109L, 131L, 168L), HbA1c.... = c(4.9, 5.3, 5.5, 5.3, 6.1, 5.7
), Total.protein..g.dl. = c(NA, 77L, 79L, 21L, 72L, 72L), Albumin..g.l. = c(NA, 
45.4, 47.5, 42.7, 43.9, 43.1), Urea..mg.dl. = c(30L, 34L, 28L, 
27L, 27L, 32L), Creatinine..mg.dl. = c(0.86, 0.97, 0.89, 0.9, 
0.74, 1.22), eGFR = c(">90", ">90", ">90", ">90", ">90", "63"
), ALT...U.l. = c(14L, 47L, 33L, 27L, 20L, 35L), AST..U.l. = c(22L, 
31L, 25L, 20L, 21L, 24L), gamma.GT..U.l. = c(NA, 45L, 22L, 15L, 
33L, 58L), LDH..U.l. = c(200L, 205L, 153L, 160L, 246L, 264L), 
    Alkaline.phosphatase..U.l. = c(52L, NA, 79L, 56L, 45L, 59L
    ), TSH.basal..mU.l. = c(3.15, 0.97, 1.43, 1.05, 0.52, 8.83
    ), Total.Bilirubin..mg.dl. = c(NA, 0.38, 1.62, 1.25, 0.45, 
    0.73), Direct.Bilirubin..mg.dl. = c(NA, 0.2, 0.55, 0.43, 
    0.2, 0.28), Total.Cholesterol..mg.dl. = c(146L, 221L, 166L, 
    169L, 207L, 149L), HDL.Cholesterol..mg.dl. = c(57L, 48L, 
    53L, 42L, 58L, 46L), Non.HDL.Cholesterol..mg.dl. = c(90L, 
    173L, 113L, 127L, 149L, 103L), LDL.Cholesterol..mg.dl. = c(86L, 
    143L, 106L, 115L, 139L, 83L), Triglycerides..mg.dl. = c(74L, 
    219L, 67L, 77L, 63L, 159L)), row.names = c(NA, 6L), class = "data.frame")

Output of summary y (excerpt):

 Height..cm.     Pulse.Rate..bpm. Systolic.Blood.Pressure Diastolic.Blood.Pressure
 Min.   :  1.71   Min.   :60.00    Min.   :100.0           Min.   :59.00           
 1st Qu.:174.00   1st Qu.:65.00    1st Qu.:113.5           1st Qu.:73.50           
 Median :182.00   Median :72.00    Median :121.0           Median :80.00           
 Mean   :156.39   Mean   :70.14    Mean   :124.0           Mean   :77.86           
 3rd Qu.:185.50   3rd Qu.:75.00    3rd Qu.:135.0           3rd Qu.:80.50           

 Max.   :192.00   Max.   :79.00    Max.   :150.0           Max.   :98.00           

You can also create a summary statistics manually in a easier to handle format. The final product is a dataframe which can be easily exported to a csv or Excel file.

library(dplyr)
df <- structure(list(
  Pseudonym = c(1L, 2L, 4L, 5L, 6L, 7L), Group = c(
    "Intervention",
    "Intervention", "Intervention", "Intervention", "Intervention",
    "Intervention"
  ), Visit = c(1L, 1L, 1L, 1L, 1L, 1L), Sex = c(
    "M",
    "M", "M", "M", "M", "M"
  ), Weight..kg. = c(
    90.2, 109.3, 78, 70.7,
    89.1, 91.2
  ), Height..cm. = c(186, 192, 173, 182, 185, 175), Pulse.Rate..bpm. = c(
    76L,
    72L, 60L, 64L, 79L, 74L
  ), Systolic.Blood.Pressure = c(
    150L, 140L,
    115L, 100L, 121L, 112L
  ), Diastolic.Blood.Pressure = c(
    80L, 80L,
    69L, 59L, 81L, 98L
  ), Respiration.Rate = c(
    20L, 16L, 13L, 16L,
    13L, 18L
  ), HS.cTnT..ng.l. = c("18", "5", "78", "6", "12", "10"), Myoglobin..ug.l. = c(NA, "31", "173", "21", "25", "63"), NT.proBNP..ng.l. = c(
    88L,
    55L, 60L, 14L, 282L, 256L
  ), CK..U.l. = c(
    53L, 137L, 115L, 72L,
    59L, 188L
  ), CK.MB = c(12.9, 14.5, 69, 15.6, 19.2, 28.2), CK.MB.... = c(
    25L,
    11L, 13L, 22L, 32L, 15L
  ), CRP..mg.l. = c(
    "5.3", "3.6", "1.2",
    "4.5", "1.1", "0.8"
  ), Hemoglobin..g.dl. = c(
    15.6, 15.1, 14.4,
    14.1, 17.3, 16.2
  ), Hematocrit..l.l. = c(
    0.437, 0.465, 0.441,
    0.427, 0.53, 0.459
  ), Erythrocytes...pl. = c(
    4.9, 5.5, 5, 5.6,
    5.8, 4.9
  ), Leucocytes...nl. = c(
    7.13, 6.75, 5.95, 8.36, 7.41,
    8.83
  ), Thromobocytes...nl. = c(
    298L, 240L, 319L, 148L, 286L,
    184L
  ), Mean.corpuscular.hemoglobin..pg. = c(
    32.2, 27.2, 29, 25.2,
    30.1, 33.3
  ), Mean.corpuscular.hemoglobin.concentration..g.dl. = c(
    35.7,
    32.5, 32.7, 33, 32.6, 35.3
  ), Mean.Corpuscular.Volume..fl. = c(
    90,
    85, 89, 76, 92, 94
  ), Mean.platelet.volume..fl. = c(
    10, 10.8,
    12.2, 10.8, 10.9, 12.3
  ), Red.cell.distribution.width..RDW.CV..... = c(
    12.5,
    13, 12.3, 12.5, 13.7, 12.3
  ), Neutrophils.absolute...nl. = c(
    3.85,
    4.04, 3.74, 5.51, 4.65, 5.16
  ), Neutrophils.... = c(
    54, 59.9,
    62.9, 65.9, 62.8, 58.6
  ), Immature...banded.granuclocytes..Neutrophils....nl. = c(
    0.06,
    0.03, 0.02, 0.04, 0.04, 0.03
  ), Immature...banded.granulocytes..Neutrophils..... = c(
    0.8,
    0.4, 0.3, 0.5, 0.5, 0.3
  ), Reticulocytes...nl. = c(
    NA, 127.8,
    56.5, 37, 74.8, 89.9
  ), Eosinophils.... = c(
    1.7, 2.8, 2.2, 0.7,
    1.2, 2
  ), Eosinophils...nl. = c(
    0.12, 0.19, 0.13, 0.06, 0.09,
    0.18
  ), Basophils.... = c(0.4, 0.6, 0.3, 0.4, 0.7, 1.1), Basophils...nl. = c(
    0.03,
    0.04, 0.02, 0.03, 0.05, 0.1
  ), Lymphocytes...nl. = c(
    2.5, 1.73,
    1.54, 2.14, 1.91, 2.72
  ), Lymphocytes.... = c(
    35.1, 25.6, 25.9,
    25.6, 25.8, 30.8
  ), Monocytes.... = c(8, 10.7, 8.4, 6.9, 9, 7.2), Monocytes...nl. = c(0.57, 0.72, 0.5, 0.58, 0.67, 0.64), Sodium..mmol.l. = c(
    137L,
    142L, 143L, 142L, 136L, 142L
  ), Potassium..mmol.l. = c(
    3.3, 4.4,
    4.2, 4.7, 4.4, 4.4
  ), Iron..umol.L. = c(
    11.9, 17.6, 18.4, 8.1,
    14.6, 25.2
  ), Ferritin..ug.l. = c(
    54.4, 244.4, 100.5, 177.9, 137.6,
    535.9
  ), Transferrin..g.l. = c(2.72, 2.43, 2.8, 2.54, 2.88, 2.14), Transferrin.Saturation.... = c(
    17.4, 28.8, 26.2, 12.7, 20.2,
    46.9
  ), Total.Calcium..mmol.l. = c(
    NA, 2.37, 2.39, 2.46, 2.33,
    2.22
  ), Calcium.corrected..mmol.l. = c(
    NA, 2.24, 2.2, 2.39, 2.23,
    2.14
  ), Chloride..mmol.l. = c(NA, 106L, 102L, 101L, 102L, 106L), Magnesium..mmol.l. = c(NA, NA, 0.86, 0.88, 0.86, 0.78), Phosphate..mmol.l. = c(
    NA,
    NA, 1.28, 1.27, 1.07, 0.9
  ), Glucose..mg.dl. = c(
    126L, 82L, 107L,
    109L, 131L, 168L
  ), HbA1c.... = c(4.9, 5.3, 5.5, 5.3, 6.1, 5.7), Total.protein..g.dl. = c(NA, 77L, 79L, 21L, 72L, 72L), Albumin..g.l. = c(
    NA,
    45.4, 47.5, 42.7, 43.9, 43.1
  ), Urea..mg.dl. = c(
    30L, 34L, 28L,
    27L, 27L, 32L
  ), Creatinine..mg.dl. = c(
    0.86, 0.97, 0.89, 0.9,
    0.74, 1.22
  ), eGFR = c(">90", ">90", ">90", ">90", ">90", "63"), ALT...U.l. = c(14L, 47L, 33L, 27L, 20L, 35L), AST..U.l. = c(
    22L,
    31L, 25L, 20L, 21L, 24L
  ), gamma.GT..U.l. = c(
    NA, 45L, 22L, 15L,
    33L, 58L
  ), LDH..U.l. = c(200L, 205L, 153L, 160L, 246L, 264L),
  Alkaline.phosphatase..U.l. = c(52L, NA, 79L, 56L, 45L, 59L), TSH.basal..mU.l. = c(3.15, 0.97, 1.43, 1.05, 0.52, 8.83), Total.Bilirubin..mg.dl. = c(
    NA, 0.38, 1.62, 1.25, 0.45,
    0.73
  ), Direct.Bilirubin..mg.dl. = c(
    NA, 0.2, 0.55, 0.43,
    0.2, 0.28
  ), Total.Cholesterol..mg.dl. = c(
    146L, 221L, 166L,
    169L, 207L, 149L
  ), HDL.Cholesterol..mg.dl. = c(
    57L, 48L,
    53L, 42L, 58L, 46L
  ), Non.HDL.Cholesterol..mg.dl. = c(
    90L,
    173L, 113L, 127L, 149L, 103L
  ), LDL.Cholesterol..mg.dl. = c(
    86L,
    143L, 106L, 115L, 139L, 83L
  ), Triglycerides..mg.dl. = c(
    74L,
    219L, 67L, 77L, 63L, 159L
  )
), row.names = c(NA, 6L), class = "data.frame")





df %>%
  filter(Group == "Intervention") %>%
  filter(Visit == 1) %>%
  as_tibble() %>%
  # convert e_gfr to numeric
  mutate_at("eGFR", ~ as.numeric(str_extract(., "[0-9]+"))) %>%
  # delesect non-numeric variables
  select(-Pseudonym, -Group, -Visit, -Sex) %>%
  # convert character values to numeric
  mutate_if(is.character, is.numeric) %>%
  # summarise
  summarise(across(everything(),
    .f = list(
      mean = mean,
      min = min,
      median = median,
      max = max, sd = sd
    ), na.rm = TRUE
  )) %>%
  # reshaping
  tidyr::pivot_longer(
    cols = everything(),
    names_sep = "_",
    names_to = c("variable", ".value")
  ) %>%
  # write as csv (writexl::write_xlsx for Excel)
  readr::write_csv(., "filename.csv")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM