簡體   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