Captain Beaky
Captain Beaky

Reputation: 3

Error: can't extract column with 'col', subscript 'col' must be size 1, not 253

I am running the following code to remove outliers from my data:

detect_outlier <- function(x) {
Quantile1 <- quantile(x, probs=.25)
Quantile3 <- quantile(x, probs=.75)
x > Quantile3 + (IQR*1.5) | x < Quantile1 - (IQR*1.5)
}
remove_outlier <- function(Data1,
                        columns=Actual, Perceived, Scenarios) {
for (col in columns)
Data2 <- Data1[!detect_outlier(Data1[[col]]), ]
}
print("Remove outliers")
print(Data1)
}
Data2<-remove_outlier(Data1)

I am getting the following error code:

Error in `Data1[[col]]`:
! Can't extract column with `col`.
✖ Subscript `col` must be size 1, not 253.

Data: structure(list(X1 = c("Actual", "34.7256759", "7.8866113", "299.6446017", "299.6446017", "212.9851346", "0.00847795", "8.9548328", "136.7408268", "8.9548328"), X2 = c("Perceived", "70.4793694", "491.8435346", "212.4416552", "212.4416552", "37.1873289", "212.4416552", "284.6791016", "2.2620939", "28.9940828"), X3 = c("Scenarios", "218.8475448", "329.7595691", "29.1732069", "1.7698322", "655.1522542", "22.0537027", "64.9946116", "1.7698322", "4.1430003")), row.names = c(NA, -10L ), class = c("tbl_df", "tbl", "data.frame"))

I have tried Googling and reading various threads but as a newbie to R I am struggling to understand what is going on / how to rectify this.

Upvotes: 0

Views: 55

Answers (0)

Related Questions