在R中使用for循环创建直方图。

huangapple go评论102阅读模式
英文:

histogram in R with for loop

问题

我有一个包含74列的数据集,我正在尝试使用for循环为每一列创建直方图。在控制台中运行时,我的代码正常工作,但当我尝试编织(knit)时,R会报告我的数据不是数字。有人知道为什么吗?我也愿意接受其他解决这个问题的方法,即绘制多个直方图(使用列名作为标识符在图上)。下面是我问题的简化表示:

library(dplyr) 

data2 <- data.frame(c(1,3,5,5,2,2,1,1,1,1),
                    c(2,4,2,3,4,5,1,2,3,3))

colnames(data2) <- c("A", "B")

for (cols in colnames(data2)) {
  data2 %>% select(cols) %>% hist()
}

再次强调,逐行运行是正常的,但当我尝试编织时,会收到以下错误消息:

"Error in hist.default(.) : 'x'必须是数值 Calls: ...freduce -> withVisible -> -> hist -> hist.default

程序终止"

有趣的是,这段代码可以正常编织:

library(dplyr)

data2 <- data.frame(c(1,3,5,5,2,2,1,1,1,1),
                    c(2,4,2,3,4,5,1,2,3,3))

colnames(data2) <- c("A", "B")

hist(data2$A)
hist(data2$B)
英文:

I have a dataset with 74 columns, and I am trying to create a histogram for each one of these in a for loop. When run in the console, my code is fine, but when I try to knit it, R says that my data is not numeric. Anyone know why? I'm also open to other solutions to this problem of plotting multiple histograms (with the column name as an identifier on the plot). Below is a simplified representation of my problem.

library(dplyr) 

data2 &lt;- data.frame(c(1,3,5,5,2,2,1,1,1,1),
                    c(2,4,2,3,4,5,1,2,3,3))

colnames(data2) &lt;- c(&quot;A&quot;, &quot;B&quot;)

for (cols in colnames(data2)) {
  data2 %&gt;% select(cols) %&gt;% hist()
}

Again, works fine line-by-line, but I end up receiving the following error when I try to knit it:

"Error in hist.default(.) : 'x' must be numeric Calls:<Anonymous> ...freduce -> withVisible -><Anonymous> -> hist -> hist.default

Execution halted"

Interestingly, this code knits fine:

library(dplyr)

data2 &lt;- data.frame(c(1,3,5,5,2,2,1,1,1,1),
                    c(2,4,2,3,4,5,1,2,3,3))

colnames(data2) &lt;- c(&quot;A&quot;, &quot;B&quot;)

hist(data2$A)
hist(data2$B)

答案1

得分: 2

You can simply use lapply.

使用lapply即可。

op <- par(mfrow=c(1, 2)) # to put histograms side by side
lapply(seq(data2), function(x)
hist(x=data2[[x]], xlab=names(data2)[x], main=paste("Histogram", names(data2)[x])))
par(op) # restore

op <- par(mfrow=c(1, 2)) # 将直方图并排放置
lapply(seq(data2), function(x)
hist(x=data2[[x]], xlab=names(data2)[x], main=paste("直方图", names(data2)[x])))
par(op) # 恢复原设置

在R中使用for循环创建直方图。

在R中使用for循环创建直方图。

英文:

You can simply use lapply.

lapply(data2, hist)

Edit: Of course you can extend this as you like, e.g. titles, labels.

op &lt;- par(mfrow=c(1, 2))  # to put histograms side by side
lapply(seq(data2), function(x) 
  hist(x=data2[[x]], xlab=names(data2)[x], main=paste(&quot;Histogram&quot;, names(data2)[x])))
par(op)  # restore

在R中使用for循环创建直方图。

答案2

得分: 1

select一个单独的列会创建一个1列数据框。hist期望一个数值向量,而不是数据框。使用pull来提取一个列作为向量:

for (cols in colnames(data2)) {
  data2 %>% pull(cols) %>% hist()
}

我也不确定你所说的“我的逐行代码运行正常”是什么意思,如果将代码从for循环中取出,我会得到相同的错误(但pull仍然正常工作):

data2 %>% select(A) %>% hist
# Error in hist.default(.) : 'x' must be numeric
英文:

selecting a single column creates a 1-column data frame. hist expects a numeric vector, not a data frame. Use pull instead to extract a column as a vector:

for (cols in colnames(data2)) {
  data2 %&gt;% pull(cols) %&gt;% hist()
}

I'm also not sure what you mean with "My line-by-line code works fine", I get the same error taking the code out of the for loop (but pull still works fine):

data2 %&gt;% select(A) %&gt;% hist
# Error in hist.default(.) : &#39;x&#39; must be numeric

答案3

得分: 1

感谢,Gregor。如果有兴趣的话,我最终用于保留列名以供参考的代码如下:

for (cols in colnames(data2)) {
  data2 %>% pull(cols) %>% hist(main = cols)
}
英文:

Thanks, Gregor. In case people are interested, my final code to keep the column names for reference is:

for (cols in colnames(data2)) {
  data2 %&gt;% pull(cols) %&gt;% hist(main = cols)
}

答案4

得分: 0

这也可以在base.R中工作,如果你喜欢。

data2 <- data.frame(c(1,3,5,5,2,2,1,1,1,1),
                    c(2,4,2,3,4,5,1,2,3,3))

colnames(data2) <- c("A", "B")

for(x in names(data2)) {
  hist(data2[,x], main = paste(x, 'distribution' ))
}
英文:

This may also work in base.R, if you like.

data2 &lt;- data.frame(c(1,3,5,5,2,2,1,1,1,1),
                    c(2,4,2,3,4,5,1,2,3,3))

colnames(data2) &lt;- c(&quot;A&quot;, &quot;B&quot;)

for(x in names(data2)) {
  hist(data2[,x], main = paste(x, &#39;distribution&#39; ))
}

huangapple
  • 本文由 发表于 2020年1月6日 22:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定