Different titles for histograms using for loop

I have a dataset like this;

> head(q_agua)
# A tibble: 6 × 11
  LOCAL DATA                  rep    OD   DBO    TB    pH    PT   CTT    CE    ST
  <chr> <dttm>              <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 P1    2020-02-21 00:00:00     1   6.4   3     6.6  7.48 0.625 240    24.5  8.00
2 P1    2020-02-21 00:00:00     2   6.4   2.3   6.8  7.42 0      36    24.8 16.0 
3 P1    2020-02-21 00:00:00     3   6.4   2.7   6.7  7.35 0.224  93    24.7 30.0 
4 P1    2020-10-07 00:00:00     1   7.8   2.2   5.5  7.12 1.29    3.5  41.0 60.0 
5 P1    2020-10-07 00:00:00     2   7.9   3.5   5.4  7.14 2.23   15    39.6 58.0 
6 P1    2020-10-07 00:00:00     3   7.9   4.2   5.5  7.13 1.83   14    39.7 68.0 
>

I was able to plot histograms individually with:

library(readxl)

q_agua <- read_xlsx("C:/Users/Nicolas/OneDrive/Documentos/PCA_relatorio_completo.xlsx")

q_agua.active <- q_agua[1:60, 4:11]
 q_agua.active <- na.omit(q_agua.active)
 
hist(q_agua.active$OD, main = paste("Histograma de OD"))
hist(q_agua.active$DBO, main = paste("Histograma de DBO"))
hist(q_agua.active$TB, main = paste("Histograma de TB"))
hist(q_agua.active$pH, main = paste("Histograma de pH"))
hist(q_agua.active$PT, main = paste("Histograma de PT"))
hist(q_agua.active$CTT, main = paste("Histograma de CTT"))
hist(q_agua.active$CE, main = paste("Histograma de CE"))
hist(q_agua.active$ST, main = paste("Histograma de ST"))

I tried to optimize the script to generate all histograms at once, like this:

par(mfrow = c(2, 4))
loop.vector <- 1:8
for(i in loop.vector) {
  x <- q_agua.active[,i]
  
  hist(x,
       main = paste("Histograma de", i))
}

And I received the error:

Error in hist.default(x, main = paste("Histograma de", i)) : 
  'x' must be numeric

I want to automate the histogram plotting using the column names as the titles.
I'm pretty new to this... Does anybody have any tips? I've been trying to read some, but i'm still stuck.

Here are two solutions for making an array of plots. The histograms are rather ugly with only six rows of data.
I could not reproduce the error you got with the for loop. The only change I made to your code was to iterate over the column names instead of the column positions. If you are still getting the error, please post the output of

str(q_agua.active)
q_agua <- read.csv("~/R/Play/Dummy.csv")
q_agua.active <- q_agua[1:6, 4:11]
q_agua.active <- na.omit(q_agua.active)

par(mfrow = c(2, 4))
loop.vector <- colnames(q_agua.active)
for(i in loop.vector) {
  x <- q_agua.active[,i]
  
  hist(x,
       main = paste("Histograma de", i))
}

library(ggplot2)

library(tidyr)
q_agua_long <- pivot_longer(cols = everything(), q_agua.active, 
                            names_to = "Param", values_to = "Value")
ggplot(q_agua_long, aes(Value)) + 
  geom_histogram(color = "white", fill = "steelblue") +
  facet_wrap(~Param, scales = "free")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2022-07-27 by the reprex package (v2.0.1)

1 Like

I keep getting the same error in the first one.

> str(q_agua.active)
tibble [58 × 8] (S3: tbl_df/tbl/data.frame)
 $ OD : num [1:58] 6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO: num [1:58] 3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB : num [1:58] 6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH : num [1:58] 7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT : num [1:58] 0.625 0 0.224 1.293 2.228 ...
 $ CTT: num [1:58] 240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE : num [1:58] 24.5 24.8 24.7 41 39.6 ...
 $ ST : num [1:58] 8 16 30 60 58 ...
 - attr(*, "na.action")= 'omit' Named int [1:2] 45 57
  ..- attr(*, "names")= chr [1:2] "45" "57"

Here's the full dataset:

> print(q_agua.active, n=58)
# A tibble: 58 × 8
      OD   DBO    TB    pH    PT    CTT    CE     ST
   <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>  <dbl>
 1   6.4 3       6.6  7.48 0.625  240    24.5   8.00
 2   6.4 2.3     6.8  7.42 0       36    24.8  16.0 
 3   6.4 2.7     6.7  7.35 0.224   93    24.7  30.0 
 4   7.8 2.2     5.5  7.12 1.29     3.5  41.0  60.0 
 5   7.9 3.5     5.4  7.14 2.23    15    39.6  58.0 
 6   7.9 4.2     5.5  7.13 1.83    14    39.7  68.0 
 7   6.8 1.9     4    6.95 0.135  120    25.2  28.0 
 8   6.8 2.1     3.7  6.85 0.199  460    24.7  48.0 
 9   6.6 1.2     4.1  6.84 0.175 1100    24.7  36.0 
10   7.3 2.4     4.7  7.2  0.380   75    23.4  12.0 
11   7.3 2.6     4.5  7.36 0.223   27    18.2  18.0 
12   7.2 2.4     4.1  7.34 0.210   29    42.8  24.0 
13   5.3 4.1     7.2  7.78 2.09   120   204.  126.  
14   5.3 4.1     7.4  7.82 0.625   27   202.  138.  
15   5.4 4.6     7.3  7.85 1.03    15   204.  142.  
16  10.3 9.5    12.6  9.96 1.83   210   121.  116.  
17  10.4 9.8    12.6  6.86 2.23  1100   121   120.  
18  10.5 6.9    12.7  7.09 1.43    15   122.  100.  
19   7.4 6.3    23    7.15 0.702 1100    84    76.0 
20   7.3 6.8    23    7.18 0.734 1100    87.6  84.0 
21   7.1 5.9    23    7.24 0.822  160    86.5  86.0 
22   7.6 6.7     9.4  7.2  0.421  120   120   100.  
23   7.5 6.5     9.4  7.43 0.450  290   121.  100.  
24   7.5 6.5     9    7.51 0.543  290   121.   78.0 
25   5.9 1.3    19.8  7.57 0.358    7.4  86.0  80.0 
26   5.9 0.800  19.9  7.62 1.69    36    86.6  86.0 
27   6   1.7    19.7  7.61 0.892   11    85.6  88.0 
28   6.2 1.3    14.6  7.33 0.892   21    92.3 116.  
29   6   2.4    14.3  7.3  3.43    93    89.1  98.0 
30   5.9 1.8    14.5  7.31 1.69     3    88.2 124.  
31   6.9 4.7   114    7.05 1.69  1100    54.5 136.  
32   7   6.1   112    7.14 1.71  1100    55.8 106.  
33   7   5.5   111    7.09 3.07  1100    55.0 172.  
34   7.6 2.6    11.9  7.55 0.284   75    95.3  64.0 
35   7.5 2.4    11.8  7.54 0.309   29    93.9 106.  
36   7.5 3.1    12.1  7.55 0.281 1100    94.3  88.0 
37   7.2 1.7    10.2  7.56 0.758  150    38.6  14.0 
38   7.3 1.8    12    7.5  0.625   11    29.8  20.0 
39   7.3 2.7    11.9  7.49 0.758   21    32.9  26.0 
40   8.1 2.8    11.4  7.05 0.892   28    26.0  72.0 
41   8.2 2.9    11    7.1  1.69     7.4  24.9  62.0 
42   8.3 2.8    11    7.11 3.16    11    24.2  82.0 
43   7.4 1.3    10.6  6.83 0.239  210    31.7  52.0 
44   7.3 1.6    11.1  6.95 0.287 1100    28.3  38.0 
45   7.3 1.5     7.3  7.66 0.258  460    39.8  46.0 
46   7.3 1.8     7.7  7.69 0.274   27    38.8  82.0 
47   7.4 0.7     7.4  7.63 0.226   36    38.5  76.0 
48   6.3 1      11.7  7.33 0.625   16    30.6  26.0 
49   6.2 0.8    11.6  7.24 0.625   43    30.3  26.0 
50   6.3 1.2    11.7  7.63 0.358   21    30.3  30.0 
51   7.9 1.7    10.9  7.15 1.03    20    29.4  52.0 
52   8   2.7    10.8  7.18 1.83    23    28.6  68.0 
53   8.1 3.2    10.8  7.15 2.23    93    28.4  62.0 
54   7.6 2.5    22    7.06 0.590 1100    32.8  72.0 
55   7.9 3.1    22    7.02 0.327 1100    33.2  50.0 
56   8   1.9    11.1  7.73 0.268 1100    41.9  30.0 
57   8.1 1.5    11.6  7.64 0.246  460    41.8  28.0 
58   8   2      11.3  7.69 0.284  290    42.3  40.0 

If you copy the following code and run it without any changes, does it work?

q_agua.active <- structure(
  list(OD = c(6.4, 6.4, 6.4, 7.8, 7.9, 7.9, 6.8, 6.8, 
              6.6, 7.3, 7.3, 7.2, 5.3, 5.3, 5.4, 10.3, 10.4, 10.5, 7.4, 7.3, 
              7.1, 7.6, 7.5, 7.5, 5.9, 5.9, 6, 6.2, 6, 5.9, 6.9, 7, 7, 7.6, 
              7.5, 7.5, 7.2, 7.3, 7.3, 8.1, 8.2, 8.3, 7.4, 7.3, 7.3, 7.3, 7.4, 
              6.3, 6.2, 6.3, 7.9, 8, 8.1, 7.6, 7.9, 8, 8.1, 8), 
       DBO = c(3, 
               2.3, 2.7, 2.2, 3.5, 4.2, 1.9, 2.1, 1.2, 2.4, 2.6, 2.4, 4.1, 4.1, 
               4.6, 9.5, 9.8, 6.9, 6.3, 6.8, 5.9, 6.7, 6.5, 6.5, 1.3, 0.8, 1.7, 
               1.3, 2.4, 1.8, 4.7, 6.1, 5.5, 2.6, 2.4, 3.1, 1.7, 1.8, 2.7, 2.8, 
               2.9, 2.8, 1.3, 1.6, 1.5, 1.8, 0.7, 1, 0.8, 1.2, 1.7, 2.7, 3.2, 
               2.5, 3.1, 1.9, 1.5, 2), 
       TB = c(6.6, 6.8, 6.7, 5.5, 5.4, 5.5, 
              4, 3.7, 4.1, 4.7, 4.5, 4.1, 7.2, 7.4, 7.3, 12.6, 12.6, 12.7, 
              23, 23, 23, 9.4, 9.4, 9, 19.8, 19.9, 19.7, 14.6, 14.3, 14.5, 
              114, 112, 111, 11.9, 11.8, 12.1, 10.2, 12, 11.9, 11.4, 11, 11, 
              10.6, 11.1, 7.3, 7.7, 7.4, 11.7, 11.6, 11.7, 10.9, 10.8, 10.8, 
              22, 22, 11.1, 11.6, 11.3), 
       pH = c(7.48, 7.42, 7.35, 7.12, 7.14, 
              7.13, 6.95, 6.85, 6.84, 7.2, 7.36, 7.34, 7.78, 7.82, 7.85, 9.96, 
              6.86, 7.09, 7.15, 7.18, 7.24, 7.2, 7.43, 7.51, 7.57, 7.62, 7.61, 
              7.33, 7.3, 7.31, 7.05, 7.14, 7.09, 7.55, 7.54, 7.55, 7.56, 7.5, 
              7.49, 7.05, 7.1, 7.11, 6.83, 6.95, 7.66, 7.69, 7.63, 7.33, 7.24, 
              7.63, 7.15, 7.18, 7.15, 7.06, 7.02, 7.73, 7.64, 7.69), 
       PT = c(0.625, 
              0, 0.224, 1.29, 2.23, 1.83, 0.135, 0.199, 0.175, 0.38, 0.223, 
              0.21, 2.09, 0.625, 1.03, 1.83, 2.23, 1.43, 0.702, 0.734, 0.822, 
              0.421, 0.45, 0.543, 0.358, 1.69, 0.892, 0.892, 3.43, 1.69, 1.69, 
              1.71, 3.07, 0.284, 0.309, 0.281, 0.758, 0.625, 0.758, 0.892, 
              1.69, 3.16, 0.239, 0.287, 0.258, 0.274, 0.226, 0.625, 0.625, 
              0.358, 1.03, 1.83, 2.23, 0.59, 0.327, 0.268, 0.246, 0.284), 
       CTT = c(240, 
               36, 93, 3.5, 15, 14, 120, 460, 1100, 75, 27, 29, 120, 27, 15, 
               210, 1100, 15, 1100, 1100, 160, 120, 290, 290, 7.4, 36, 11, 21, 
               93, 3, 1100, 1100, 1100, 75, 29, 1100, 150, 11, 21, 28, 7.4, 
               11, 210, 1100, 460, 27, 36, 16, 43, 21, 20, 23, 93, 1100, 1100, 
               1100, 460, 290), 
       CE = c(24.5, 24.8, 24.7, 41, 39.6, 39.7, 25.2, 
              24.7, 24.7, 23.4, 18.2, 42.8, 204, 202, 204, 121, 121, 122, 84, 
              87.6, 86.5, 120, 121, 121, 86, 86.6, 85.6, 92.3, 89.1, 88.2, 
              54.5, 55.8, 55, 95.3, 93.9, 94.3, 38.6, 29.8, 32.9, 26, 24.9, 
              24.2, 31.7, 28.3, 39.8, 38.8, 38.5, 30.6, 30.3, 30.3, 29.4, 28.6, 
              28.4, 32.8, 33.2, 41.9, 41.8, 42.3), 
       ST = c(8, 16, 30, 60, 58, 
              68, 28, 48, 36, 12, 18, 24, 126, 138, 142, 116, 120, 100, 76, 
              84, 86, 100, 100, 78, 80, 86, 88, 116, 98, 124, 136, 106, 172, 
              64, 106, 88, 14, 20, 26, 72, 62, 82, 52, 38, 46, 82, 76, 26, 
              26, 30, 52, 68, 62, 72, 50, 30, 28, 40)), 
  class = "data.frame", row.names = c(NA, -58L))
par(mfrow = c(2, 4))
loop.vector <- colnames(q_agua.active)
for(i in loop.vector) {
  x <- q_agua.active[,i]
  
  hist(x,
       main = paste("Histograma de", i))
}

If it does not work, please post the result I asked for previously, the output of

str(q_agua.active)

This code works. What may i been doing wrong?

> str(q_agua.active)
'data.frame':	58 obs. of  8 variables:
 $ OD : num  6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO: num  3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB : num  6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH : num  7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT : num  0.625 0 0.224 1.29 2.23 1.83 0.135 0.199 0.175 0.38 ...
 $ CTT: num  240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE : num  24.5 24.8 24.7 41 39.6 39.7 25.2 24.7 24.7 23.4 ...
 $ ST : num  8 16 30 60 58 68 28 48 36 12 ...

If you are getting the error that "x must be numeric", then at least one of your columns is being converted to characters. You can see from the output of str() that all of the columns of q_agua.active are numeric for the current version of that data frame. I suggest you run your code with calls to str() after each step.

q_agua <- read_xlsx("C:/Users/Nicolas/OneDrive/Documentos/PCA_relatorio_completo.xlsx")
str(q_agua)
q_agua.active <- q_agua[1:60, 4:11]
str(q_agua.active)
q_agua.active <- na.omit(q_agua.active)
str(q_agua.active)
par(mfrow = c(2, 4))
loop.vector <- colnames(q_agua.active)
for(i in loop.vector) {
  x <- q_agua.active[,i]
  
  hist(x,
       main = paste("Histograma de", i))
}

Check that the columns that will be plotted are numeric at each step.

Do any of these throw an error?

hist(q_agua.active$OD, main = paste("Histograma de OD"))
hist(q_agua.active$DBO, main = paste("Histograma de DBO"))
hist(q_agua.active$TB, main = paste("Histograma de TB"))
hist(q_agua.active$pH, main = paste("Histograma de pH"))
hist(q_agua.active$PT, main = paste("Histograma de PT"))
hist(q_agua.active$CTT, main = paste("Histograma de CTT"))
hist(q_agua.active$CE, main = paste("Histograma de CE"))
hist(q_agua.active$ST, main = paste("Histograma de ST")

No. All good.

> hist(q_agua.active$OD, main = paste("Histograma de OD"))
> hist(q_agua.active$DBO, main = paste("Histograma de DBO"))
> hist(q_agua.active$TB, main = paste("Histograma de TB"))
> hist(q_agua.active$pH, main = paste("Histograma de pH"))
> hist(q_agua.active$PT, main = paste("Histograma de PT"))
> hist(q_agua.active$CTT, main = paste("Histograma de CTT"))
> hist(q_agua.active$CE, main = paste("Histograma de CE"))
> hist(q_agua.active$ST, main = paste("Histograma de ST"))

Try

for(i in loop.vector) {
  x <- q_agua.active[,i]
  print(head(x))
  #hist(x,
   #    main = paste("Histograma de", i))
}

If you do not see non-numeric results, restart RStudio, since I'll be out of ideas.

> q_agua <- read_xlsx("C:/Users/Nicolas/OneDrive/Documentos/PCA_relatorio_completo.xlsx")
> str(q_agua)
tibble [60 × 11] (S3: tbl_df/tbl/data.frame)
 $ LOCAL: chr [1:60] "P1" "P1" "P1" "P1" ...
 $ DATA : POSIXct[1:60], format: "2020-02-21" "2020-02-21" "2020-02-21" "2020-10-07" ...
 $ rep  : num [1:60] 1 2 3 1 2 3 1 2 3 1 ...
 $ OD   : num [1:60] 6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO  : num [1:60] 3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB   : num [1:60] 6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH   : num [1:60] 7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT   : num [1:60] 0.625 0 0.224 1.293 2.228 ...
 $ CTT  : num [1:60] 240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE   : num [1:60] 24.5 24.8 24.7 41 39.6 ...
 $ ST   : num [1:60] 8 16 30 60 58 ...

> q_agua.active <- q_agua[1:60, 4:11]
> str(q_agua.active)
tibble [60 × 8] (S3: tbl_df/tbl/data.frame)
 $ OD : num [1:60] 6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO: num [1:60] 3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB : num [1:60] 6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH : num [1:60] 7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT : num [1:60] 0.625 0 0.224 1.293 2.228 ...
 $ CTT: num [1:60] 240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE : num [1:60] 24.5 24.8 24.7 41 39.6 ...
 $ ST : num [1:60] 8 16 30 60 58 ...

>  q_agua.active <- na.omit(q_agua.active)
> str(q_agua.active)
tibble [58 × 8] (S3: tbl_df/tbl/data.frame)
 $ OD : num [1:58] 6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO: num [1:58] 3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB : num [1:58] 6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH : num [1:58] 7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT : num [1:58] 0.625 0 0.224 1.293 2.228 ...
 $ CTT: num [1:58] 240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE : num [1:58] 24.5 24.8 24.7 41 39.6 ...
 $ ST : num [1:58] 8 16 30 60 58 ...
 - attr(*, "na.action")= 'omit' Named int [1:2] 45 57
  ..- attr(*, "names")= chr [1:2] "45" "57"

> par(mfrow = c(2, 4))
> loop.vector <- colnames(q_agua.active)
> for(i in loop.vector) {
+     x <- q_agua.active[,i]
+     print(head(x))
+     #hist(x,
+     #    main = paste("Histograma de", i))
+ }
# A tibble: 6 × 1
     OD
  <dbl>
1   6.4
2   6.4
3   6.4
4   7.8
5   7.9
6   7.9
# A tibble: 6 × 1
    DBO
  <dbl>
1   3  
2   2.3
3   2.7
4   2.2
5   3.5
6   4.2
# A tibble: 6 × 1
     TB
  <dbl>
1   6.6
2   6.8
3   6.7
4   5.5
5   5.4
6   5.5
# A tibble: 6 × 1
     pH
  <dbl>
1  7.48
2  7.42
3  7.35
4  7.12
5  7.14
6  7.13
# A tibble: 6 × 1
     PT
  <dbl>
1 0.625
2 0    
3 0.224
4 1.29 
5 2.23 
6 1.83 
# A tibble: 6 × 1
    CTT
  <dbl>
1 240  
2  36  
3  93  
4   3.5
5  15  
6  14  
# A tibble: 6 × 1
     CE
  <dbl>
1  24.5
2  24.8
3  24.7
4  41.0
5  39.6
6  39.7
# A tibble: 6 × 1
     ST
  <dbl>
1  8.00
2 16.0 
3 30.0 
4 60.0 
5 58.0 
6 68.0 

Got this...

> q_agua <- read_xlsx("C:/Users/Nicolas/OneDrive/Documentos/PCA_relatorio_completo.xlsx")
> str(q_agua)
tibble [60 × 11] (S3: tbl_df/tbl/data.frame)
 $ LOCAL: chr [1:60] "P1" "P1" "P1" "P1" ...
 $ DATA : POSIXct[1:60], format: "2020-02-21" "2020-02-21" "2020-02-21" "2020-10-07" ...
 $ rep  : num [1:60] 1 2 3 1 2 3 1 2 3 1 ...
 $ OD   : num [1:60] 6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO  : num [1:60] 3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB   : num [1:60] 6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH   : num [1:60] 7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT   : num [1:60] 0.625 0 0.224 1.293 2.228 ...
 $ CTT  : num [1:60] 240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE   : num [1:60] 24.5 24.8 24.7 41 39.6 ...
 $ ST   : num [1:60] 8 16 30 60 58 ...

> q_agua.active <- q_agua[1:60, 4:11]
> str(q_agua.active)
tibble [60 × 8] (S3: tbl_df/tbl/data.frame)
 $ OD : num [1:60] 6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO: num [1:60] 3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB : num [1:60] 6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH : num [1:60] 7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT : num [1:60] 0.625 0 0.224 1.293 2.228 ...
 $ CTT: num [1:60] 240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE : num [1:60] 24.5 24.8 24.7 41 39.6 ...
 $ ST : num [1:60] 8 16 30 60 58 ...

> q_agua.active <- na.omit(q_agua.active)
> str(q_agua.active)
tibble [58 × 8] (S3: tbl_df/tbl/data.frame)
 $ OD : num [1:58] 6.4 6.4 6.4 7.8 7.9 7.9 6.8 6.8 6.6 7.3 ...
 $ DBO: num [1:58] 3 2.3 2.7 2.2 3.5 4.2 1.9 2.1 1.2 2.4 ...
 $ TB : num [1:58] 6.6 6.8 6.7 5.5 5.4 5.5 4 3.7 4.1 4.7 ...
 $ pH : num [1:58] 7.48 7.42 7.35 7.12 7.14 7.13 6.95 6.85 6.84 7.2 ...
 $ PT : num [1:58] 0.625 0 0.224 1.293 2.228 ...
 $ CTT: num [1:58] 240 36 93 3.5 15 14 120 460 1100 75 ...
 $ CE : num [1:58] 24.5 24.8 24.7 41 39.6 ...
 $ ST : num [1:58] 8 16 30 60 58 ...
 - attr(*, "na.action")= 'omit' Named int [1:2] 45 57
  ..- attr(*, "names")= chr [1:2] "45" "57"

> par(mfrow = c(2, 4))
> loop.vector <- colnames(q_agua.active)
> for(i in loop.vector) {
+     x <- q_agua.active[,i]
+     print(head(x))
+     #hist(x,
+     #    main = paste("Histograma de", i))
+ }
# A tibble: 6 × 1
     OD
  <dbl>
1   6.4
2   6.4
3   6.4
4   7.8
5   7.9
6   7.9
# A tibble: 6 × 1
    DBO
  <dbl>
1   3  
2   2.3
3   2.7
4   2.2
5   3.5
6   4.2
# A tibble: 6 × 1
     TB
  <dbl>
1   6.6
2   6.8
3   6.7
4   5.5
5   5.4
6   5.5
# A tibble: 6 × 1
     pH
  <dbl>
1  7.48
2  7.42
3  7.35
4  7.12
5  7.14
6  7.13
# A tibble: 6 × 1
     PT
  <dbl>
1 0.625
2 0    
3 0.224
4 1.29 
5 2.23 
6 1.83 
# A tibble: 6 × 1
    CTT
  <dbl>
1 240  
2  36  
3  93  
4   3.5
5  15  
6  14  
# A tibble: 6 × 1
     CE
  <dbl>
1  24.5
2  24.8
3  24.7
4  41.0
5  39.6
6  39.7
# A tibble: 6 × 1
     ST
  <dbl>
1  8.00
2 16.0 
3 30.0 
4 60.0 
5 58.0 
6 68.0 
>

Still not seeing non-numeric results.

Already restarted and updated RStudio.

I really don't know what's happening, but I wanna learn.
Thanks for your time FJCC.

Ah, a tibble/data frame difference I did not know. This works.

for(i in loop.vector) {
  x <- q_agua.active[,i]
  hist(x[[1]],
      main = paste("Histograma de", i))
}
1 Like

NICE!

q_agua <- read_xlsx("C:/Users/Nicolas/OneDrive/Documentos/PCA_relatorio_completo.xlsx")
str(q_agua)
q_agua.active <- q_agua[1:60, 4:11]
str(q_agua.active)
q_agua.active <- na.omit(q_agua.active)
str(q_agua.active)
par(mfrow = c(2, 4))
loop.vector <- colnames(q_agua.active)
for(i in loop.vector) {
  x <- q_agua.active[,i]
  
  hist(x[[1]],
       main = paste("Histograma de", i),
       xlab = i)
}

THANK YOU SO MUCH!!!

This topic was automatically closed 7 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.