0

跟进问题在数据框中搜索唯一值并使用它们创建表

这是我的数据的样子

    UUID    Source
1   Jane    http//mywebsite.com44bb00?utm_source=ADW&utm_medium=banner&utm_campaign=Monk&gclid1234
2   Mike    http//mywebsite.com44bb00?utm_source=Google&utm_medium=cpc&utm_campaign=DOG&gclid1234
3   John    http//mywebsite.com44bb00?utm_source=Yahoo&utm_medium=banner&utm_campaign=DOG&gclid1234
4   Sarah   http//mywebsite.com44bb00?utm_source=Facebookdw&utm_medium=cpc&utm_campaign=CAT&gclid1234
5   Michael http//mywebsite.com44bb00?utm_source=Twitter&utm_medium=GDNr&utm_campaign=CAT&gclid1234
6   Bob     http//mywebsite.com44bb00?utm_source=ADW&utm_medium=GDN&utm_campaign=DOG&gclid1234
7   Mark    http//mywebsite.com44bb00?utm_source=Twitter&utm_medium=banner&utm_campaign=MONK&gclid1234
8   Anna    http//mywebsite.com44bb00?utm_source=Facebook&utm_medium=banner&utm_campaign=MONK&gclid1234

这是我想要实现的期望输出

    NAME    UTM_SOURCE  UTM_MEDIUM  UTM_CAMPAIGN
1   Jane    ADW             banner     Monk
2   Mike    Google          cpc        DOG
3   John    Yahoo           banner     DOG
4   Sarah   Faceboo         cpc        CAT
5   Michael Twitter         GDN        CAT
6   Bob     ADW             GDN        DOG
7   Mark    Twitter         banner     MONK
8   Anna    Facebook        banner     MONK

所以换句话说,我想要的是根据标准获取特定的信息。示例:在数据框中搜索值“utmsource=”,找到后复制在“=”和“&”符号之间找到的任何信息。在用户 no1 (Jame) 的情况下,如果您查看原始文件,她的源 URL 包含值“utm_source=ADW”。在输出文件中,“ADW”位被提取并归入一个名为“utm_source”的新列中。所有其他用户和其他维度的相同原则(utm_medium 和 utm_campaign)

我知道该功能gsub可以帮助我。这是我到目前为止所尝试的:

> file1 <- read.csv("C:/Users/Dumitru Ostaciu/Desktop/Users.csv")
> file1 <- transform(file1, Source = as.character(Source))
> file2 <- gsub(".*\\?utm_source=", "", file1$Source)

这就是我得到的结果

  UUID  SOURCE
    1   ADW&utm_medium=banner&utm_campaign=Monk&gclid1234
    2   Google&utm_medium=cpc&utm_campaign=DOG&gclid1234
    3   Yahoo&utm_medium=banner&utm_campaign=DOG&gclid1234
    4   Facebookdw&utm_medium=cpc&utm_campaign=CAT&gclid1234
    5   Twitter&utm_medium=GDNr&utm_campaign=CAT&gclid1234
    6   ADW&utm_medium=GDN&utm_campaign=DOG&gclid1234
    7   Twitter&utm_medium=banner&utm_campaign=MONK&gclid1234
    8   Facebook&utm_medium=banner&utm_campaign=MONK&gclid1234   

我对此有两个问题:

1) 在我得到的输出中,该函数复制了值 "utm_source-" 后面的所有内容。如何添加另一个维度以使公式仅复制“=”和“&”之间的内容

2) 我如何保留最初在第一列 (UUID)、Jane、Mike、John 等中的值?

4

2 回答 2

1

你需要做两件事:

  1. 用于gsub从您的来源中删除网站名称
  2. 用于strsplit在每次出现时分隔剩余的字符串?

读入数据:

x <- read.table(text="
UUID    Source
1   Jane    http//mywebsite.com44bb00?utm_source=ADW&utm_medium=banner&utm_campaign=Monk&gclid1234
2   Mike    http//mywebsite.com44bb00?utm_source=Google&utm_medium=cpc&utm_campaign=DOG&gclid1234
3   John    http//mywebsite.com44bb00?utm_source=Yahoo&utm_medium=banner&utm_campaign=DOG&gclid1234
4   Sarah   http//mywebsite.com44bb00?utm_source=Facebookdw&utm_medium=cpc&utm_campaign=CAT&gclid1234
5   Michael http//mywebsite.com44bb00?utm_source=Twitter&utm_medium=GDNr&utm_campaign=CAT&gclid1234
6   Bob     http//mywebsite.com44bb00?utm_source=ADW&utm_medium=GDN&utm_campaign=DOG&gclid1234
7   Mark    http//mywebsite.com44bb00?utm_source=Twitter&utm_medium=banner&utm_campaign=MONK&gclid1234
8   Anna    http//mywebsite.com44bb00?utm_source=Facebook&utm_medium=banner&utm_campaign=MONK&gclid1234", header=TRUE, stringsAsFactors=FALSE)

用于strsplit在每个分隔源字符串?

z <- matrix(
  unlist(strsplit(gsub(".*\\?", "", x$Source), "\\&")), 
  ncol=4, byrow=TRUE)
cbind(x$UUID, gsub(".*=", "", z))

     [,1]      [,2]         [,3]     [,4]   [,5]       
[1,] "Jane"    "ADW"        "banner" "Monk" "gclid1234"
[2,] "Mike"    "Google"     "cpc"    "DOG"  "gclid1234"
[3,] "John"    "Yahoo"      "banner" "DOG"  "gclid1234"
[4,] "Sarah"   "Facebookdw" "cpc"    "CAT"  "gclid1234"
[5,] "Michael" "Twitter"    "GDNr"   "CAT"  "gclid1234"
[6,] "Bob"     "ADW"        "GDN"    "DOG"  "gclid1234"
[7,] "Mark"    "Twitter"    "banner" "MONK" "gclid1234"
[8,] "Anna"    "Facebook"   "banner" "MONK" "gclid1234"

然后转换为数据框并添加名称:

z <- matrix(
  unlist(strsplit(gsub(".*\\?", "", x$Source), "\\&")), 
  ncol=4, byrow=TRUE)
z <- cbind(x$UUID, gsub(".*=", "", z))
z <- as.data.frame(z[, -5])
names(z) <- c("UUID", "UTM_SOURCE", "UTM_MEDIUM", "UTM_CAMPAIGN")
z

     UUID UTM_SOURCE UTM_MEDIUM UTM_CAMPAIGN
1    Jane        ADW     banner         Monk
2    Mike     Google        cpc          DOG
3    John      Yahoo     banner          DOG
4   Sarah Facebookdw        cpc          CAT
5 Michael    Twitter       GDNr          CAT
6     Bob        ADW        GDN          DOG
7    Mark    Twitter     banner         MONK
8    Anna   Facebook     banner         MONK
于 2012-11-20T12:09:01.560 回答
1

这是我的做法

> file1 <- read.csv("C:/Users/Dumitru Ostaciu/Desktop/Users.csv")
> file1 <- transform(file1, Source = as.character(Source))
> z <- matrix(
     unlist(strsplit(gsub(".*\\?", "", file1$Source), "\\&")), 
     ncol=4, byrow=TRUE)
> file2 <- cbind(file1$UUID, gsub(".*=", "", z))

这是我得到的结果

    V1  V2          V3      V4      V5
1   3   ADW         banner  Monk    gclid1234
2   7   Google      cpc     DOG     gclid1234
3   4   Yahoo       banner  DOG     gclid1234
4   8   Facebookdw  cpc     CAT     gclid1234
5   6   Twitter     GDNr    CAT     gclid1234
6   2   ADW         GDN     DOG     gclid1234
7   5   Twitter     banner  MONK    gclid1234
8   1   Facebook    banner  MONK    gclid1234

我需要指出,我的真实数据将有 500.000 行,并且在第一列中会有一个唯一的名称。

如何解决此问题以使名称显示在 V1 中?我的错误是什么?

于 2012-11-20T12:42:34.500 回答