> colnames(fileIWantToAnalyze)
[1] "variable_1a" "variable_5b"
[3] "variable_1b" "variable_6a"
[5] "variable_2a" "variable_6b"
[7] "variable_2b" "variable_7a"
[9] "variable_3a" "variable_7b"
[11] "variable_3b" "variable_8a"
[13] "variable_4a" "variable_8b"
[15] "variable_4b" "variable_9a"
[17] "variable_5a" "variable_9b"
[19] "GroupingColumn1"
I am not able to run the following code in R - throws this error:
"fileIWantToAnalyze" is a data.tableError in [.data.table(fileIWantToAnalyze, , .(mean1 = mean(get(attribute)), : The items in the 'by' or 'keyby' list are length (943026,1). Each must be length 943026; the same length as there are rows in x (after subsetting if i is provided).
for(attribute in colnames(fileIWantToAnalyze)[c(1,3,5,7,9,11,13,15,17)]){
fileIWantToAnalyze[,.(mean1 = mean(get(attribute)),count1 = .N),
by = .(GroupingColumn1,sub("a", "b", attribute))]
}
This doesn't work too
for (attribute in colnames(fileIWantToAnalyze)[c(1,3,5,7,9,11,13,15,17)]){
fileIWantToAnalyze[,.(mean1 = mean(get(attribute)),count1 = .N),
by = .(GroupingColumn1,attribute)]
}
The following code gives me the answer I am looking for - but I want to use a loop to generate outputs for many variables
fileIWantToAnalyze[,.(mean1 = mean(variable_1a),count1 = .N),
by = .(GroupingColumn1,variable_1b))]
I believe the problem is how I am calling the the "attribute" in the 'by' command while grouping