How to deal with NA output of apply in R?

R ProgrammingServer Side ProgrammingProgramming

When we use apply function on numerical as well as character column then the output of the function returns NA for all hence to deal with this problem, we can use lapply function. The lapply function will take each column into account independently, therefore, the arithmetic operations will be performed individually.

Check out the below given examples to understand how it works.

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-LETTERS[1:20]
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created −

     x1          x2         x3
1   1.8023520   0.02949008  A
2  -0.6755818   0.03066978  B
3   0.8067009  -1.42504773  C
4  -0.6978446  -0.03589390  D
5  -0.9768782  -2.29225371  E
6   0.7589207  -0.56158056  F
7   1.4361048   0.05587417  G
8   2.2569459  -1.64800771  H
9   1.0998291  -0.67096333  I
10 -0.7250079   1.02007341  J
11 -1.6972296   0.25851780  K
12  2.6042033   1.13646450  L
13  1.3473008  -0.24734074  M
14  0.5262951  -0.56210383  N
15 -0.4576343   0.32515395  O
16  1.2211340   1.18324358  P
17  0.3792386  -0.52471110  Q
18 -0.8220452   0.28614199  R
19 -0.5467174  -1.88444207  S
20 -0.7032419  -1.01928201  T

In order to find the mean of all columns in df1 using apply function, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-LETTERS[1:20]
df1<-data.frame(x1,x2,x3)
apply(df1,2,mean)

Output

If you execute all the above given snippets as a single program, it generates the following output −

x1 x2 x3
NA NA NA
Warning messages:
1: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA

The correct way is to use lapply instead of apply. Add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-LETTERS[1:20]
df1<-data.frame(x1,x2,x3)
lapply(df1,mean)

Output

If you execute all the above given snippets as a single program, it generates the following output −

$x1
[1] 0.3468422

$x2
[1] -0.3272999

$x3
[1] NA
Warning message:
In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA

Example 2

Following snippet creates a sample data frame −

ID<-letters[1:20]
Response<-rpois(20,5)
df2<-data.frame(ID,Response)
df2

The following dataframe is created −

   ID Response
1  a  8
2  b  5
3  c  4
4  d  4
5  e  4
6  f  2
7  g  3
8  h  4
9  i  6
10 j  3
11 k  6
12 l  2
13 m  5
14 n  3
15 o  4
16 p  7
17 q  6
18 r  6
19 s  6
20 t  7

To find the mean of all columns in df2 using apply function, add the following code to the above snippet −

ID<-letters[1:20]
Response<-rpois(20,5)
df2<-data.frame(ID,Response)
apply(df2,2,mean)

Output

If you execute all the above given snippets as a single program, it generates the following output −

ID Response
NA NA
Warning messages:
1: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA

To find the mean of all columns in df2 using apply function, add the following code to the above snippet −

ID<-letters[1:20]
Response<-rpois(20,5)
df2<-data.frame(ID,Response)
lapply(df2,mean)

Output

If you execute all the above given snippets as a single program, it generates the following output −

$ID
[1] NA

$Response
[1] 4.75

Warning message:
In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
raja
Published on 12-Nov-2021 07:56:43

Advertisements