I have an issue with performing a t-test over all columns of my dataframe.
What I want to do? Each column represents a KPI of a certain research question. Moreover each column can be divided into two groups target = 1 and non-target = 0, defined in the "target" column. I want to perform a t-test per column between those two groups and save the p-value and t-value in a separate dataframe.
Below you can find a code which would work, there would be no error message. However I would need to type the code below 70 times column by column and always change the column name (below "X1y_ret"), which is not convenient as I have those 70 columns in my dataframe.
t.test(X1y_ret ~ target, data = analysis_matched_targets_all, var.equal = FALSE)$p.value
That's why I tried to use a for loop to make it more convenient. You can find the code below. However it doesn't work, giving me an error message of different variable lengths (found for 'target').
for(i in colnames(analysis_matched_targets_all) {
statistics_all_types_all_year[3, which(colnames(analysis_matched_targets_all)==i )] <- t.test(i ~ target, data = analysis_matched_targets_all, var.equal = FALSE)$p.value}
I would really appreciate if you could help me with my problem :)