简体   繁体   中英

how to apply function to each column in dataframe of R

I want to apply a function to each column in R. Suppose following is the dataframe with (3xn):

df <- data.frame(
  h1 = c(1,2,3),
  h2 = c(2,3,1),
  h3 = c(3,2,1),
  h4 = c(1,2,3),
  h5 = c(1,2,3)
)
rownames(df) <- c("e1", "e2", "e3")
df
#    h1 h2 h3 h4 h5
# e1  1  2  3  1  1
# e2  2  3  2  2  2
# e3  3  1  1  3  3

if we want to check if the first 2 elements suppose (e1==1, e2==2) for each column (h1,h2...). How could we apply the checking function to each column in the data frame?

Subset the rows of the data based on either row.names or the head , compare == with a vector of values, get the colSums of the logical matrix derived from it and check if that is equal to 2 ie if both the elements are TRUE for each column

colSums(mat[c("e1", "e2"),] == c(1, 2))==2

Or with apply , loop over the columns (MARGIN = 2 ), apply the function (anonymous function call) and check if all are TRUE from the comparison

apply(head(mat, 2), 2, function(x) all(x  == c(1, 2)))

Using @alex_jwb90's data,

lapply(df, function(a) a[1:2] == 1:2)
# $h1
# [1] TRUE TRUE
# $h2
# [1] FALSE FALSE
# $h3
# [1] FALSE  TRUE
# $h4
# [1] TRUE TRUE
# $h5
# [1] TRUE TRUE

lapply(df, function(a) all(a[1:2] == 1:2))
# $h1
# [1] TRUE
# $h2
# [1] FALSE
# $h3
# [1] FALSE
# $h4
# [1] TRUE
# $h5
# [1] TRUE

sapply(df, function(a) all(a[1:2] == 1:2))
#    h1    h2    h3    h4    h5 
#  TRUE FALSE FALSE  TRUE  TRUE 

You can try this (extensible to check more than two rows if you remove the & row_number() <= 2 )

library(dplyr)

df = tibble(
  h1 = c(1,2,3),
  h2 = c(2,3,1),
  h3 = c(3,2,1),
  h4 = c(1,2,3),
  h5 = c(1,2,3)
)

df %>%
  mutate_all(
    list(equals_rownum = ~.==row_number() & row_number() <= 2)
  )

If you don't want to create new columns <col>_equals_rownum but replace h1,h2,...-columns, just remove the name in the list -call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM