Search code examples
rdplyrrow

Applying function to every row using a range of columns (R)


My data contains consecutive columns V1-V1998 with other columns at either side of these. I want to calculate the skewness of the rows within this range of 1998 columns.

Here is the code I tried:

ND2a <- NoDup2 %>%
  rowwise() %>%
  mutate(skew2 = skewness(V1:V1998))

This creates a new column called skew2 however the skewness isn't calculated and instead the column is filled with "NaN". Does anyone know why this might be?

I'm using skewness from the moments package.

My data looks a little like this

Data                         V1       V2        V3    .....   V1998  ....
Acaricomes phytoseiuli        0.01    0.0       0.002         0.03
Acetivibrio cellulolyticus    0.005   0.002     0.011         0.04
Acetobacter aceti             0.001   0.003     0.004         0.0

Solution

  • You can do:

    library(e1071)
    
    # get column names
    cols <- paste0('V', seq(1,1998,1))
    
    # apply function on selected columns
    NoDup2$skew_value <- apply(NoDup2[,cols], 1, skewness)
    

    With this we calculate skewness for every row across all columns in the given data set.