Huh, in this case perhaps xtabs()
isn't the best option since it doesn't result in a data frame that can be easily exported. The alternative is:
library(tidyverse)
# create a similar data frame for demonstration
df <- tibble(
sample_name = c("tom", "john"),
first_box = c("loose", "ugly"),
second_box = c("good color", "off color")
)
my_table <- df %>%
pivot_longer(c(first_box, second_box), names_to = "box", values_to = "word") %>%
count(sample_name, word) %>%
pivot_wider(names_from = word, values_from = n, values_fill = list(n = 0))
my_table
#> # A tibble: 2 x 5
#> sample_name `off color` ugly `good color` loose
#> <chr> <int> <int> <int> <int>
#> 1 john 1 1 0 0
#> 2 tom 0 0 1 1
write_excel_csv(my_table, "my_table.csv")
Created on 2020-05-25 by the reprex package (v0.3.0)
Now the global environment becomes
Also you should see the file my_table.csv
under the root directory available for excel.