Question

I have following data frame:

df <- read.table(text="
Word1            Word2              Word3            Total.Impressions
best           budget             laptop                 1
laptop           budget           computer               1
laptop           sales            budget                 2
budget          windows           laptop                 1
laptop          budget           laptop                 1
                 ",
                 header=TRUE,as.is=TRUE)

I need all "budget" in Word1 column, and "laptop" in Word2 column and rest in Word3 column.

Expected output:

 Word1  Word2    Word3 Total.Impressions
budget laptop     best                 1
budget laptop computer                 1
budget laptop    sales                 2
budget laptop  windows                 1
budget laptop   laptop                 1
Was it helpful?

Solution

Not clear what you trying to achieve but this should get you started...

output <- 
  data.frame(
    Word1="budget",
    Word2="laptop",
    Word3=sapply(1:nrow(df),function(i){
      x <- as.character(df[i,1:3])
      x[ !x %in% c("budget","laptop")]}),
    Total.Impressions=df$Total.Impressions
    )

EDIT: Possibly there is a prettier way of doing this, but this should work:

output <- 
  data.frame(
    Word1="budget",
    Word2="laptop",
    Word3=sapply(1:nrow(df),function(i){
      x <- as.character(df[i,1:3])
      res <- x[ !x %in% c("budget","laptop")]
      #check if result is not empty
      if(length(res)==0){
        res <- aggregate(x,list(x),length)
        res[ res$x==2, 1]}
      else res
      }),
    Total.Impressions=df$Total.Impressions
    )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top