【发布时间】:2015-01-05 07:20:31
【问题描述】:
我制作了一个适合我的 R 脚本,但我知道我可以通过使用函数使它变得更好(更漂亮)。不幸的是,我的各种尝试都没有成功。谁能引导我走上正确的道路?以下是我的原始脚本。
library(dplyr)
apples <- read.csv("JoburgApples.csv")
grs <- apples %>% filter(grepl("GRANNY", ProductName), tvaluesold >10000) %>% mutate(Variety = "Granny Smith")
cpp <- apples %>% filter(grepl("PINK", ProductName), tvaluesold >10000) %>% mutate(Variety = "Cripps Pink")
top <- apples %>% filter(grepl("TOP", ProductName), tvaluesold >10000) %>% mutate(Variety = "Top Red")
gld <- apples %>% filter(grepl("GOLDEN", ProductName), tvaluesold >10000) %>% mutate(Variety = "Golden Delicious")
ski <- apples %>% filter(grepl("STARKING", ProductName), tvaluesold >10000) %>% mutate(Variety = "Starking")
bra <- apples %>% filter(grepl("BRAEBURN", ProductName), tvaluesold >10000) %>% mutate(Variety = "Braeburn")
apples <- rbind(grs, cpp, top, gld, ski, bra)
s70 <- apples %>% filter(grepl("70$", ProductName)) %>% mutate(Count = 70)
s80 <- apples %>% filter(grepl("80$", ProductName)) %>% mutate(Count = 80)
s90 <- apples %>% filter(grepl("90$", ProductName)) %>% mutate(Count = 90)
s100 <- apples %>% filter(grepl("100$", ProductName)) %>% mutate(Count = 100)
s110 <- apples %>% filter(grepl("110$", ProductName)) %>% mutate(Count = 110)
s120 <- apples %>% filter(grepl("120$", ProductName)) %>% mutate(Count = 120)
s135 <- apples %>% filter(grepl("135$", ProductName)) %>% mutate(Count = 135)
s150 <- apples %>% filter(grepl("150$", ProductName)) %>% mutate(Count = 150)
s165 <- apples %>% filter(grepl("165$", ProductName)) %>% mutate(Count = 165)
apples <- rbind(s70, s80, s90, s100, s110, s120, s135, s150, s165)
编辑。链接到 .csv 文件 (https://github.com/fderyckel/showcases/blob/master/JoburgMarket/JoburgApples.csv)
> UnitMass ProductName tvaluesold tquantitysold tkgsold avgprice highestprice date
> 18.50KG CARTON CRIPPS PINK,CL 1,100 200 1 18.5 200 200 06/11/14
> 18.50KG CARTON CRIPPS RED,CL 1,70 200 1 18.5 200 200 06/11/14
> 18.50KG CARTON TOPRED,CL 1,180 1300 10 185 130 130 06/11/14
> 18.50KG CARTON GOLDEN DELICIOUS,CL 1,90 22700 108 1998 210.19 240 06/11/14
> 18.50KG CARTON STARKING,CL 1,80 17920 115 2127.5 155.83 230 06/11/14
> 18.50KG CARTON GRANNY SMITH,CL 1,135 1800 12 222 150 150 06/11/14
> 18.50KG CARTON TOPRED,CL 1,90 1730 12 222 144.17 190 06/11/14
> 18.50KG CARTON CRIPPS PINK,CL 1,90 2600 13 240.5 200 200 06/11/14
> 18.50KG CARTON GOLDEN DELICIOUS,CL 1,120 22800 136 2516 167.65 180 06/11/14
> 18.50KG CARTON GOLDEN DELICIOUS,CL 1,135 21810 136 2516 160.37 180 06/11/14
> 18.50KG CARTON GRANNY SMITH,CL 1,70 2380 14 259 170 220 06/11/14
> 18.50KG CARTON GRANNY SMITH,CL 1,165 1200 15 277.5 80 80 06/11/14
提前感谢您的帮助。
弗朗索瓦
【问题讨论】:
-
您可以将所有这些
grepl调用放在一个调用中。阅读?regex手册页和|运算符。这些都是向量化的操作,但是没有任何数据就很难提供帮助 -
感谢@RichardScriven。我已经用 .csv 的链接和文件的前几行更新了问题。
-
谢谢@docendodiscimus。我确实使用文件链接和前几行更新了问题。
-
@akrun 非常感谢。
-
@Franky 您应该已经指定您需要
ProductName的第一部分和最后一部分。我浪费时间追你的代码。看你的代码"Top Red",在原始数据集中是TOPRED。所以,我的解决方案给了你显示的结果。