在R中创建框图的循环

发布于 2025-02-11 15:36:52 字数 1265 浏览 0 评论 0原文

我有一个包含数字和分类值的数据集。我正在尝试创建框图,以在我的数据集中的每个数字列上视觉上识别异常值。以下代码可以做到这一点,但它非常笨拙,我不想使用此代码,其中包含更多变量。我正在寻找一种使用循环使用R中的循环创建框图的方法

。这是无循环的笨拙的代码:

#Using Boxplots, check for outliers in each in each float or integer value column. 

b <-boxplot(df$item1, main = 'item1')
b <-boxplot(df$item2, main = 'item2')
b <-boxplot(df$item3, main = 'item3')
b <-boxplot(df$item4, main = 'item4')
b <-boxplot(df$item5, main = 'item5')
b <-boxplot(df$item6, main = 'item6')
b <-boxplot(df$item7, main = 'item7')
b <-boxplot(df$item8, main = 'item8')
b <-boxplot(df$item9, main = 'item9')
b <-boxplot(df$item10, main = 'item10')
b <-boxplot(df$item11, main = 'item11')
b <-boxplot(df$item12, main = 'item12')
b <-boxplot(df$item13, main = 'item13')
b <-boxplot(df$item14, main = 'item14')
b <-boxplot(df$item15, main = 'item15')
b <-boxplot(df$item16, main = 'item16')

在Python中,代码将是:(

outliers = ['Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Item6', 'Item7', 'Item8', 'Item9', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16']

i=0 
while i < len(outliers):
    sns.boxplot(x = outliers[i], data = df)
    plt.show()
    i = i + 1

我在R中寻找类似的东西!) 谢谢你!

I have a dataset that contains both numeric and categorical values. I am trying to create box plots to visually identify outliers for each numeric column in my dataset. The below code works to do this, but it is very clunky and I would not want to use this code with even more variables. I am looking for a way to use a loop to create box plots using a loop in R.

Here is the clunky code that works without a loop:

#Using Boxplots, check for outliers in each in each float or integer value column. 

b <-boxplot(df$item1, main = 'item1')
b <-boxplot(df$item2, main = 'item2')
b <-boxplot(df$item3, main = 'item3')
b <-boxplot(df$item4, main = 'item4')
b <-boxplot(df$item5, main = 'item5')
b <-boxplot(df$item6, main = 'item6')
b <-boxplot(df$item7, main = 'item7')
b <-boxplot(df$item8, main = 'item8')
b <-boxplot(df$item9, main = 'item9')
b <-boxplot(df$item10, main = 'item10')
b <-boxplot(df$item11, main = 'item11')
b <-boxplot(df$item12, main = 'item12')
b <-boxplot(df$item13, main = 'item13')
b <-boxplot(df$item14, main = 'item14')
b <-boxplot(df$item15, main = 'item15')
b <-boxplot(df$item16, main = 'item16')

In python the code would be:

outliers = ['Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Item6', 'Item7', 'Item8', 'Item9', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16']

i=0 
while i < len(outliers):
    sns.boxplot(x = outliers[i], data = df)
    plt.show()
    i = i + 1

(I am looking for something similar in R!)
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2025-02-18 15:36:52

使用循环在列上循环,并基于mtcars的最小preprex,您可以做

outliers <- c("mpg", "hp")

for (i in outliers) {
  boxplot(mtcars[i], main = i)
}

”“ ”

Using a for loop to loop over the columns and a minimal reprex based on mtcars you could do

outliers <- c("mpg", "hp")

for (i in outliers) {
  boxplot(mtcars[i], main = i)
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文