R apply user define function on data frame columns

user3628777

in R I have a function define to calculate intersection between 2 strings:

containedin <- function(t1,t2){
  return length(Reduce(intersect, strsplit(c(t1,t2), "\\s+"))) 
}

I want to apply this function on a data frame that contains 2 string columns: data.selected[c('keywords','title')]

keywords                                                                             title
1  Samsung UN48H6350 48" Samsung UN48H6350 48" Full 1080p Smart HDTV 120Hz with Wi-Fi +$50 Visa Gift Card
2  Samsung UN48H6350 48"     Samsung UN48H6350 48" Full HD Smart LED TV -Bundle- (See Below for Contents)
3  Samsung UN48H6350 48"      Samsung UN48H6350 48" Class Full HD Smart LED TV -BUNDLE- See below Details
4  Samsung UN48H6350 48"     Samsung UN48H6350 48" Full HD Smart LED TV With BD-H5100 Blu-ray Disc Player
5  Samsung UN48H6350 48"                 Samsung UN48H6350 48" Smart 1080p Clear Motion Rate 240 LED HDTV
6  Samsung UN48H6350 48"            Samsung UN48H6350 - 48-Inch Full HD 1080p Smart HDTV 120Hz with Wi-Fi
7  Samsung UN48H6350 48"               Samsung 6350 Series UN48H6350 48" 1080p HD LED LCD Internet TV NEW
8  Samsung UN48H6350 48"  Samsung Un48h6350af 75" 1080p Led-lcd Tv - 16:9 - Hdtv 1080p - (un75h6350afxza)
9  Samsung UN48H6350 48"                         Samsung UN48H6350 - 48" HD 1080p Smart HDTV 120Hz Bundle
10 Samsung UN48H6350 48"   Samsung UN48H6350 - 48-Inch Full HD 1080p Smart HDTV 120Hz with Wi-Fi, (R#416)

How do I use the apply function to be applied on these 2 columns, to return a new column with the result ?

shadow

First of all, your return statement should really give you an error. You probably mean

containedin <- function(t1,t2){
  length(Reduce(intersect, strsplit(c(t1,t2), "\\s+"))) 
}

Anyway, you can use mapply to solve your problem.

mapply(containedin, 
       as.character(data.selected[, 'keywords']), 
       as.character(data.selected[, 'title']))

The as.character is only necessary if class(data.selected[, 'keywords']) is factor (instead of character)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R data.table apply function to rows using columns as arguments

From Dev

How do I apply a function in R to certain columns of a data frame grouped by another column?

From Dev

How to apply a function on a specific column in the data frame using R?

From Dev

Using apply() function to update the factor levels of multiple columns of a data frame in R

From Dev

Apply function to subset of data frame

From Dev

How to apply a function that works with data.frame cells to data.frame columns

From Dev

R Apply function on data frame columns

From Dev

Assign results of apply to multiple columns of data frame

From Dev

R apply user define function on data frame columns

From Dev

Apply function row wise on pandas data frame on columns with numerical values

From Dev

Apply custom function to two columns for every row in data frame in R

From Dev

apply a function in data frame with if/else statement in R

From Dev

R: apply-like function that returns a data frame?

From Dev

How to apply this function for multiple elements in a data frame in r?

From Dev

Difference between apply and sapply for data frame columns?

From Dev

How to apply a function on a specific column in the data frame using R?

From Dev

R apply conversion to multiple columns of data.frame

From Dev

Using apply() function to update the factor levels of multiple columns of a data frame in R

From Dev

Split data frame into groups by time and apply a function to multiple columns using R

From Dev

How to apply a function that works with data.frame cells to data.frame columns

From Dev

Using function to obtain the densities of numeric columns in a data frame in R

From Dev

Apply a function data frame column

From Dev

apply a function in data frame with if/else statement in R

From Dev

Using dplyr to apply a function of several columns of an R data frame

From Dev

Apply function in data frame

From Dev

Apply function to frame of characters in R

From Dev

how to apply duplicated function to the columns of a data frame

From Dev

R: Apply function on data frame A dependent on values of data frame B

From Dev

R: Apply function to data frame including only specific rows

Related Related

  1. 1

    R data.table apply function to rows using columns as arguments

  2. 2

    How do I apply a function in R to certain columns of a data frame grouped by another column?

  3. 3

    How to apply a function on a specific column in the data frame using R?

  4. 4

    Using apply() function to update the factor levels of multiple columns of a data frame in R

  5. 5

    Apply function to subset of data frame

  6. 6

    How to apply a function that works with data.frame cells to data.frame columns

  7. 7

    R Apply function on data frame columns

  8. 8

    Assign results of apply to multiple columns of data frame

  9. 9

    R apply user define function on data frame columns

  10. 10

    Apply function row wise on pandas data frame on columns with numerical values

  11. 11

    Apply custom function to two columns for every row in data frame in R

  12. 12

    apply a function in data frame with if/else statement in R

  13. 13

    R: apply-like function that returns a data frame?

  14. 14

    How to apply this function for multiple elements in a data frame in r?

  15. 15

    Difference between apply and sapply for data frame columns?

  16. 16

    How to apply a function on a specific column in the data frame using R?

  17. 17

    R apply conversion to multiple columns of data.frame

  18. 18

    Using apply() function to update the factor levels of multiple columns of a data frame in R

  19. 19

    Split data frame into groups by time and apply a function to multiple columns using R

  20. 20

    How to apply a function that works with data.frame cells to data.frame columns

  21. 21

    Using function to obtain the densities of numeric columns in a data frame in R

  22. 22

    Apply a function data frame column

  23. 23

    apply a function in data frame with if/else statement in R

  24. 24

    Using dplyr to apply a function of several columns of an R data frame

  25. 25

    Apply function in data frame

  26. 26

    Apply function to frame of characters in R

  27. 27

    how to apply duplicated function to the columns of a data frame

  28. 28

    R: Apply function on data frame A dependent on values of data frame B

  29. 29

    R: Apply function to data frame including only specific rows

HotTag

Archive