rsyntaxvariable-lengthfrequency-table

Creating Table in R Using List of Values from Another Dataset with Different Length


I have a dataset of 50,000+ observations and I'm trying to create a table of my two variables of interest, chemical and lab_code. The code below outputs a working table that can be used to create a basic heatmap, but like the the main dataset, it only contains 10 lab codes.

table(lab_data$chemical, lab_data$lab_code) 

I have a txt file simply listing all 30 lab codes, but I'm struggling to create a table that take arguments of different lengths and will have many columns of zeros. The length of lab_data$chemical is 50,000+ and the length of codes$lab_code is 30.

table(lab_data$chemical, codes$lab_code)

Error in table(lab_data$chemical, codes$lab_code) : all arguments must have the same length


Solution

  • table(lab_data$chemical, factor(lab_data$lab_code, levels(as.factor(codes$lab_code))))