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