Home > Net >  How to get lables within a loop
How to get lables within a loop

Time:06-07

Here is part of my sample. Please assume that I have more than 12 ds

dat<-read.table (text=" d1  d2  d3  d4  d5  d6  d7  d8  d9  d10 d11 d12
    100 100 60  50  100 100 20  60  100 100 100 20
    75  75  60  50  75  75  20  100 75  75  75  20
    75  40  60  50  41  41  20  100 75  75  75  75
    78  49  60  50  45  41  20  100 75  75  75  75
    75  50  60  50  50  48  20  60  75  75  75  75
    75  48  60  50  44  49  20  40  10  75  75  10
    20  10  60  50  100 100 20  100 100 100 100 20
    75  75  60  50  75  75  20  50  75  39  75  10
    75  75  60  50  75  75  20  90  75  75  75  40
    22  10  60  50  75  75  20  90  75  10  75  10
    ", header=TRUE)

I want to get this table within the loop.

D   sd
d1  25.44711
d2  29.01647
d3  0
d4  0
d5  22.11586
d6  22.26831
d7  0
d8  23.78141
d9  24.61368
d10 26.89672
d11 10.54093
d12 28.62109

The loop is:

for(i in seq_along(dat)) {
  k<- dat[[i]]
  if(class(k) %in%  c("numeric", "integer"))  
    print(sd(k))
  else print("F")
}

CodePudding user response:

You don't need a loop for this:

data.frame(
    D = names(dat),
    sd = sapply(dat, sd) 
)
#       D       sd
# d1   d1 25.44711
# d2   d2 29.01647
# d3   d3  0.00000
# d4   d4  0.00000
# d5   d5 22.11586
# d6   d6 22.26831
# d7   d7  0.00000
# d8   d8 23.78141
# d9   d9 24.61368
# d10 d10 26.89672
# d11 d11 10.54093
# d12 d12 28.62109

CodePudding user response:

Here's an option with dplyr and purrr

library(dplyr)
library(purrr)

map_df(dat, ~sd(.)) %>% 
  pivot_longer(everything()) %>% 
  rename(D = 1, sd = 2)

Which gives:

# A tibble: 12 × 2
   D        sd
   <chr> <dbl>
 1 d1     25.4
 2 d2     29.0
 3 d3      0  
 4 d4      0  
 5 d5     22.1
 6 d6     22.3
 7 d7      0  
 8 d8     23.8
 9 d9     24.6
10 d10    26.9
11 d11    10.5
12 d12    28.6

CodePudding user response:

Here is a loop solution -

out <- data.frame(D = names(dat), sd = 0)

for(i in seq_along(dat)) {
  k<- dat[[i]]
  if(class(k) %in%  c("numeric", "integer"))  
   out$sd[i] <- sd(k)
  else 
    out$sd[i] <- NA #"F"
}

out
#     D     sd
#1   d1 25.447
#2   d2 29.016
#3   d3  0.000
#4   d4  0.000
#5   d5 22.116
#6   d6 22.268
#7   d7  0.000
#8   d8 23.781
#9   d9 24.614
#10 d10 26.897
#11 d11 10.541
#12 d12 28.621

Note that if class of the column is not numeric or integer I am assigning NA to sd column to maintain the class. You can assign "F" as well instead of NA as well but it will change the class of the column to character.

CodePudding user response:

You can also solve it in the following way:

results <- c()
nms <- c()
for(i in seq_along(dat)){
  
  if(inherits(i, c('integer', 'numeric'))){
    nms<- c(nms, names(dat)[i])
    results <- c(results, sd(dat[[i]]))
  }
}
data.frame(nms, sd = results)

   nms       sd
1   d1 25.44711
2   d2 29.01647
3   d3  0.00000
4   d4  0.00000
5   d5 22.11586
6   d6 22.26831
7   d7  0.00000
8   d8 23.78141
9   d9 24.61368
10 d10 26.89672
11 d11 10.54093
12 d12 28.62109
  •  Tags:  
  • r
  • Related