繁体   English   中英

如何根据R中另一列中的特定值获取特定列的所有值?

[英]How to get all values of a specific column based on a specific value in another column in R?

我的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'中的'Campaign'列出'geo_fl.csv'中的所有Adgroup,就像'Camp.csv'中的佛罗里达一样,它应该返回'geo_fl.csv'中的值(orlando,miami)

到目前为止代码如下 -

 # 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新手,任何帮助表示赞赏。

目前还不清楚你希望你的输出看起来像什么,但这是一种简单的方法来连接属于另一个因素的每个级别的一个因子的所有级别:

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 )。

编辑:(澄清所需的输出后)

上面的代码返回一个连接字符串,其中包含新列表中存在的每个Campaign中存在的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'])

在要求输出后,

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的需要,只要你在第二张表中拥有所有独特的广告系列。 我只是做了dummmy数据在这里,其中x为你的campaigny是你的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可能更好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM