Hi all, I have a two part question, might be related to how my data is formatted (I am still new to R)
I have data for 10 different ID, each have 15 measurements for size, and PB, the data is arranged in 3 columns:
Example of the dataframe:
data.frame(
stringsAsFactors = FALSE,
ID = c("PS_orange","PS_orange",
"PS_orange","PS_orange","PS_orange","PS_orange",
"PS_orange","PS_orange","PS_orange","PS_orange","PS_orange",
"PS_orange","PS_orange","PS_orange","PS_orange","PET",
"PET","PET","PET","PET"),
Size = c(299L,116L,85L,228L,56L,
128L,113L,75L,118L,299L,71L,235L,133L,237L,50L,
261L,239L,179L,116L,156L),
PB = c(217.95,255,229.44,255,255,
255,255,222.97,255,255,255,235.3,255,205.9,206.8,
90.67,196.8,204,173.1,118)
I wanted to calculate descriptive statistics for all ID both for size and PB and I used:
Li25 <- by(df, df$ID, summary)
This turns back all the descriptive statistics for each ID separately, which is great.
However, when I tried to export the summary statistics and tried to convert the results into a data frame
25df <- as.data.frame(by(df, df$ID, summary))
I get an error message of:
Error in as.data.frame.default(by(df, df$ID, summary)) :
cannot coerce class ‘"by"’ to a data.frame
I have tried other ways to export the summary statistics but have been unsuccessful.
Do you have any resolution for this?
Another part is related to the CI calculations using the t.test function.
In order to run the t.test for each ID I split the data frame
split1 <- split(df, df$ID)
and ran the t.test for each ID separately
t.test(split1[["PS_orange"]]$PB)
t.test(split1[["PET"]]$PB)
This works well but is a bit crumbemsome, I am sure there is a way to tell R to run these in one go and I tried
split(df, df$ID) %>%
lapply(., {
t.test(PB)
})
which turns back
Error in t.test(PB) : object 'PB' not found
If you have suggestions especially to overcome the manual transferring of summary statistics into excel, this would be much appreciated, thanks