简体   繁体   中英

How to extract same texts/values from 2 columns in R data frame?

I want to extract text/value that is same in col1 and col2 , and create "desired_col" as provided in my data frame. I tried few things but did not work ..

mydata_1<-data.frame(col1=c("SL1234","SL786876"),col2=c("SL1334","SL78076"),desired_col=c(c("SL1","SL78")))

An option using mapply as:

mydata_1$matched <- mapply(function(x,y){
  # First take same length fron both columns
  x <- substring(x,1, min(nchar(x),nchar(y)))
  y <- substring(y,1, min(nchar(x),nchar(y)))

  matching_len <- which(strsplit(x, split = "")[[1]] != strsplit(y, split = "")[[1]])[1]-1
  substring(x, 1, matching_len)
}, mydata_1$col1, mydata_1$col2)


mydata_1
#       col1    col2 desired_col matched
# 1   SL1234  SL1334         SL1     SL1
# 2 SL786876 SL78076        SL78    SL78

Data:

mydata_1<-data.frame(col1=c("SL1234","SL786876"),
                     col2=c("SL1334","SL78076"),
                     desired_col=c(c("SL1","SL78")), 
                     stringsAsFactors = FALSE)

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