I am using tapply to combine a table by Sample ID(SID). For the first sample on the list there are 3 measurements but it appears as only one.
I have 4 things that need to pass to the new table. First is SID. Second is the mean of the areas for all measurements that have that SID. Third is all the Distances. Finally the number of measurements.
cases_iTLS <- data.frame(unique(iTLS$SID))
colnames(cases_iTLS)[colnames(cases_iTLS)=="unique.iTLS.SID."] <- "SID"
cases_iTLS$SID <- factor(cases_iTLS$SID)
# Average of TLS on one slide for area
cases_iTLS$Area_iTLS <- tapply(iTLS$Area, iTLS$SID,FUN=mean)
# Average of TLS on one slide for distance
cases_iTLS$Distance_iTLS <- tapply(iTLS$Distance, iTLS$SID,FUN=mean)
# Number of measurements per SID
cases_iTLS$Count_iTLS <- tapply(iTLS$Region_Index, iTLS$SID,FUN=length)
SID Region_index Area Distance Type Location
112906 1 53531.53 71.982 iTLS intratumoral
112906 3 76809.61 97.384 iTLS intratumoral
112906 5 40937.30 9.643 iTLS intratumoral
112947 1 35071.66 2.067 iTLS intratumoral
112947 3 17979.88 36.319 iTLS