我的CSV文件格式为
Camp.CSV
Campaign,AdGroup,Keyword,Status
florida,orlando,floridaorlando,Paused
new york,albany,new yorkalbany,Active
geo_fl.csv
Campaign,Adgroup
florida,orlando
florida,miami
new york,new york
california,san francisco,
california,los angeles
我想根据'Camp.csv'中的'广告'列出'geo_fl.csv'中的所有广告组 'Camp.csv'中的佛罗里达州应该在'geo_fl.csv'中返回值(奥兰多,迈阿密)
到目前为止代码如下 -
# Declare function to check with the presence of the 'campaignname' or not
campaignname <- function(point1, point2) {
conditioncheck <- any(point2==point1)
}
# Declare a function to check the presence of the 'adgroupname' or not
# Read the CSV files for reference
newlistings <- read.csv("/home/chi/Downloads/Camp.csv",header=TRUE)
georeportrecord <- read.csv("/home/chi/Downloads/geo_fl.csv",header=TRUE)
# Store the data of each column in a variable for 'Camp.csv'
Keyword <- newlistings$keyword
campaign <- newlistings$Campaign
adgroup <- newlistings$AdGroup
status <- newlistings$Status
# Store the data of each column in a variable for 'geo_fl.csv'
geoCampaign <- georeportrecord$Campaign
geoAdGroup <- georeportrecord$Adgroup
# getting the values for 'number of rows' in each CSV list
nCGM <- nrow(newlistings)
nAdwords <- nrow(georeportrecord)
Pts2 <- georeportrecord[,c("Campaign")]
CGMGeoList <- NULL
# checking for the presence of the element in the vector
#for(i in campaign){
for(i in 1:nCGM){
Pts1 <- NULL
Pts1$Campaign <- (newlistings$Campaign[i])
# passing the value to the function for 'campaign' presence check
checkcondition <- campaignname(Pts1,Pts2)
if(checkcondition == TRUE){
ad <- geoAdgroup[which(geoCampaign==i)# Stuck here(returning no result)
}
}
我也试过
for(i in campaign)
{ if (any(geoCampaign==i) == TRUE){
print(i)
# But also I want to list all adgroup for 'geo_fl.csv' together.
} }
我想要的输出
Campaign,AdGroup,Keyword,Status,Campaignpresentingeo_fl,Adgrouppresentingeo_fl
florida,orlando,floridaorlando,Paused,YES,YES
new york,albany,new yorkalbany,Active,YES,NO
上述期望结果的条件
for(i in campaign){
If(( i present in georeportrecord)==TRUE))#for that particular 'campaign' in 'Camp.csv' check the condition for 'Adgroup' in 'geo_fl.csv'
{ If ((AdGroup[i] present in georeportrecord$Adgroup)==TRUE))#AdGroup for that particular 'campaign' 'i' in 'Camp.csv' is also present as an adgroup in 'geo_fl.csv'
{
output write.csv(florida,orlando,floridaorlando,Paused,YES,YES)
}else{
write.csv(florida,orlando,floridaorlando,Paused,YES,NO)
}
}else{write.csv(florida,orlando,floridaorlando,Paused,NO,NO)
}
将数据输出到CSV文件中,Camp.csv中只有2个列,表示YES和NO 如何列出上面指定的值,以便 我可以写到另一个CSV文件,请帮助我以下,R新手,任何帮助表示赞赏。
答案 0 :(得分:1)
目前还不清楚你希望输出看起来像什么,但这是一种简单的方法来连接属于另一个因素的每个级别的一个因素的所有级别:
georeportrecord <- read.csv(text='Campaign,Adgroup
florida,orlando
florida,miami
new york,new york
california,san francisco
california,los angeles', header=TRUE)
newlistings <- read.csv(text='Campaign,AdGroup,Keyword,Status
florida,orlando,floridaorlando,Paused
new york,albany,new yorkalbany,Active', header=TRUE)
out <- aggregate(subset(georeportrecord,
Campaign %in% newlistings$Campaign)$Adgroup,
list(Campaign=subset(georeportrecord,
Campaign %in% newlistings$Campaign)$Campaign),
paste0)
out
Campaign x
1 florida orlando, miami
2 new york new york
使用write.csv
将数据写入csv(请参阅?write.csv
)。
编辑:(澄清所需的输出后)
上述代码返回一个连续字符串,其中包含newlistings
中存在的每个广告系列中的广告组。根据OP的要求提交:
newlistings$Campaignpresentingeo_fl <-
newlistings$Campaign %in% georeportrecord$Campaign
newlistings$Adgrouppresentingeo_fl <-
apply(newlistings, 1, function(x) x[2] %in%
subset(georeportrecord, Campaign==x[1])[, 'Adgroup'])
答案 1 :(得分:1)
在需要输出后,
x<-read.csv(text='Campaign,Adgroup
florida,orlando
florida,miami
new york,new york
california,san francisco
california,los angeles', header=T, stringsAsFactors=F)
y=read.csv(text="Campaign,AdGroup,Keyword,Status
florida,orlando,floridaorlando,Paused
new york,albany,new yorkalbany,Active", header=T, stringsAsFactors=F)
Campaigns<-x$Campaign
AdGroups<-interaction(x$Campaign, x$Adgroup)
y$campaignpresence<-ifelse(y$Campaign %in% Campaigns,"YES", "NO")
y$geopresence<-ifelse(interaction(y$Campaign, y$AdGroup) %in% AdGroups,"YES", "NO")
输出
y
Campaign AdGroup Keyword Status campaignpresence geopresence
1 florida orlando floridaorlando Paused YES YES
2 new york albany new yorkalbany Active YES NO
忽略下面,因为它回答了单独的事情
data.table的另一种方法。如果您在第二个表格中拥有所有唯一广告系列,我甚至不会看到第一个表camp.csv
的需要。我刚刚在这里制作了一些愚蠢的数据x
是你的campaign
而y
就是你的Adgroup
require(data.table)
x<-data.frame(x=sample(1:10, 100, replace=T), y=sample(100:999,100))
y<-data.table(x)
l<-y[,list(y=list(y)),by=x]
l$y<-sapply(l$y, paste, collapse=",")
write.table(l,...)
小心写作csv,因为你的第二列现在有逗号,所以tsv可能更好