Appending for loop results to data frame

I wrote code to clean data then append it to a data frame but it doesn't seem to work

MTTB <- read_excel("GG78 MODEL/MTTB.xlsx")
MTTB_Clean <- MTTB
col_names <- names(MTTB_Clean)
names(MTTB_Clean) <- col_names

#Removing outliers and cleaninG Data Set

dataframe <- data.frame()

for (i in 1:4) {
Di <- MTTB_Clean[!(MTTB_Clean[,i] %in% c(boxplot.stats(MTTB_Clean[,i])$out)),i]
dataframe <- rbind(dataframe, Di)

names(dataframe)<- c("CR01_MTBF","CR02_MTBF","CR03_MTBF","CR04_MTBF")

The line

Di <- MTTB_Clean[!(MTTB_Clean[,i] %in% c(boxplot.stats(MTTB_Clean[,i])$out)),i]

returns a single column. You will end up with a one-column data frame with about four times as many rows as your original data frame. Have you considered replacing the outliers with NA?

Be careful with this method of cleaning data. If your data set is large, some points will surely fail the boxplot.stats outlier rule, even if the data are perfectly normal. For a small data set, you are relying on the data being normally distributed.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.