apply() func. not functioning on my dataset

here is a small part of dataset:

Time Andaman and Nicobar Islands Arunachal Pradesh
1951 3275.1 3354.2
1952 3079.9 2396.1
1953 2721.9
1954 3449
1955 3349.6 5063.5
1956 3080 4195.5
1957 2507.2 3709.4

when i type: apply(file, 1, mean), it gives NA for every row along with the following warnings:
1: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA

Please help!

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

@Akash01 Well, d1 is name of dataset. d1[,-1] means that doesn't contain first column if
you don't need to contain the first column. Maybe you need the mean value of every year?

apply(d1[,-1],1,function(x) mean(x, na.rm = TRUE))

Created on 2023-06-01 with reprex v2.0.2

file2<-tibble::tribble(
         ~Time, ~Andaman.and.Nicobar.Islands, ~Arunachal.Pradesh, ~Assam.and.Meghalaya,
         1951L,                       3275.1,             3354.2,               2613.8,
         1952L,                       3079.9,             2396.1,               2851.3,
         1953L,                       2721.9,                 NA,               2559.9,
         1954L,                         3449,                 NA,               2859.1,
         1955L,                       3349.6,             5063.5,               2761.2,
         1956L,                         3080,             4195.5,               2802.9
         )

# intention to calculate mean for each year 
f.mean<-apply(file2[,-1], 1,function(x) mean(x, na.rm = TRUE))
f.mean
#> [1] 3081.033 2775.767 2640.900 3154.050 3724.767 3359.467

Created on 2023-06-01 with reprex v2.0.2

Output
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[20] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[39] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[58] NA NA NA NA NA NA NA NA NA NA NA NA NA
Warning: In mean.default(x, na.rm = TRUE) :
argument is not numeric or logical: returning NA

file2<-tibble::tribble(
         ~Time, ~Andaman.and.Nicobar.Islands, ~Arunachal.Pradesh, ~Assam.and.Meghalaya,
         1951,                       3275.1,             3354.2,               2613.8,
         1952,                       3079.9,             2396.1,               2851.3,
         1953,                       2721.9,                 NA,               2559.9,
         1954,                         3449,                 NA,               2859.1,
         1955,                       3349.6,             5063.5,               2761.2,
         1956,                         3080,             4195.5,               2802.9
         )

# intention to calculate mean for each year 
f.mean<-apply(file2, 1, mean)

Created on 2023-06-01 with reprex v2.0.2
It is showing the following output:

f.mean
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[20] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[39] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[58] NA NA NA NA NA NA NA NA NA NA NA NA NA

Along with this warning:
In mean.default(x, na.rm = TRUE) :
argument is not numeric or logical: returning NA

With the data provided, you should get six values, not 70. Here is what I get with and without using na.rm = TRUE.

file2<-tibble::tribble(
  ~Time, ~Andaman.and.Nicobar.Islands, ~Arunachal.Pradesh, ~Assam.and.Meghalaya,
  1951,                       3275.1,             3354.2,               2613.8,
  1952,                       3079.9,             2396.1,               2851.3,
  1953,                       2721.9,                 NA,               2559.9,
  1954,                         3449,                 NA,               2859.1,
  1955,                       3349.6,             5063.5,               2761.2,
  1956,                         3080,             4195.5,               2802.9
)

# intention to calculate mean for each year 
f.mean <- apply(file2, 1, mean)
f.mean
#> [1] 2798.525 2569.825       NA       NA 3282.325 3008.600

f.mean2 <- apply(file2, 1, function(x) mean(x,na.rm = TRUE))
f.mean2
#> [1] 2798.525 2569.825 2411.600 2754.033 3282.325 3008.600

Created on 2023-06-01 with reprex v2.0.2
Notice that I did not exclude the Time column, though doing that should not change the basic solution.

Hi, I run the code like this, it works ok on my computer. And maybe you don't need to contain the variable"time"(the first column) to get the mean value. Based on your code, maybe you have other columns that data type is not numeric like the time column. And would you want to calculate the mean value by row or by column?
Looking forward to your reply, thank you.

file2<-tibble::tribble(
  ~Time, ~Andaman.and.Nicobar.Islands, ~Arunachal.Pradesh, ~Assam.and.Meghalaya,
  1951L,                       3275.1,             3354.2,               2613.8,
  1952L,                       3079.9,             2396.1,               2851.3,
  1953L,                       2721.9,                 NA,               2559.9,
  1954L,                         3449,                 NA,               2859.1,
  1955L,                       3349.6,             5063.5,               2761.2,
  1956L,                         3080,             4195.5,               2802.9
)
f.mean<-apply(file2[,-1], 1,function(x) mean(x, na.rm = TRUE))
f.mean
#> [1] 3081.033 2775.767 2640.900 3154.050 3724.767 3359.467

Created on 2023-06-02 with reprex v2.0.2

And you could use file2$mean<-apply(file2[,-1], 1,function(x) mean(x, na.rm = TRUE)) to calcluate and add the mean value to the data, if you need to calculate the mean value by row.

file2<-tibble::tribble(
  ~Time, ~Andaman.and.Nicobar.Islands, ~Arunachal.Pradesh, ~Assam.and.Meghalaya,
  1951L,                       3275.1,             3354.2,               2613.8,
  1952L,                       3079.9,             2396.1,               2851.3,
  1953L,                       2721.9,                 NA,               2559.9,
  1954L,                         3449,                 NA,               2859.1,
  1955L,                       3349.6,             5063.5,               2761.2,
  1956L,                         3080,             4195.5,               2802.9
)
file2$mean<-apply(file2[,-1], 1,function(x) mean(x, na.rm = TRUE))

Created on 2023-06-02 with reprex v2.0.2

Thank you for your support :relieved:@Comede_way @FJCC , I am good to go.

1 Like

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