在R中的一列中排列相同的值

时间:2014-05-15 11:14:41

标签: r

我有以下数据框:

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)

我需要所有&#34;预算&#34;在Word1专栏中,&#34;笔记本电脑&#34;在Word2列中,在Word3列中休息。

预期产出:

 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

1 个答案:

答案 0 :(得分:1)

不清楚你想要实现的目标,但这应该让你开始......

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
    )

编辑:可能有一种更漂亮的方式,但这应该有效:

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
    )