本文介绍了如何在csv文件中从不同的函数一个接一个地写数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个CSV文件,根据我执行的功能,将数据写入另一个CSV文件 Data.csv 身份,州,城市,商务名称,商业名称 12,加利福尼亚州,洛杉矶,Ray兄弟,IT 34,texas,达拉斯,abc,TV 45,华盛顿,西雅图,Microsft,软件 我的输出是像 BusinessId,Campaignname 12,geo | California | Los Angeles 12,cat | Ray兄弟,IT 34,geo | texas |达拉斯 34,cat | abc | TV 45,geo | washington | seattle 45,cat | Microsoft | b 如何按照我想要的方式格式化输出? 我的代码看起来像 newlistings< - read.csv(Data.csv,header = TRUE) Identity< - newlistings $ Identity campaignname< - paste(geo,|,(tolower(as.character(newlistings $ state))),| as.character(newlistings $ Market))),sep =) ... 再次应为campaignname campaignname 我想实现它,而不使用for循环。 解决方案我认为创建一个新的数据框架是有点误导,所以我鹅卵石一起 cat - 基于解决方案:(可以轻松地修改以写入文件。) cat(cat('BusinessId,Campaignname','\\\'), apply(Data.csv,1,function(ln){ cat(ln [1 ], cat(ln [1],',',',','粘贴(cat,ln [4],ln [5],sep =|,\\\)})) BusinessId,Campaignname 12,geo |加利福尼亚州|洛杉矶 12,cat | Ray brothers | IT 34,geo | texas |达拉斯 34,cat | abc | TV 45,geo | washington | 45,cat | Microsft | Software I have a CSV file where I want to write the data onto another CSV file based on the functions I perform like Data.csvIdentity,State,City,BusinessName,BusinessNeed12,California,Los Angeles,Ray brothers,IT34,texas,Dallas,abc,TV45,washington,seattle,Microsft,SoftwareMy desrired output is likeBusinessId,Campaignname12,geo|California|Los Angeles12,cat|Ray brothers,IT34,geo|texas|Dallas34,cat|abc|TV45,geo|washington|seattle45,cat|Microsoft|SoftwareHow should I be formatting the output like the way I want?My code looks like newlistings <- read.csv("Data.csv",header=TRUE)Identity <- newlistings$Identitycampaignname <- paste("geo","|",(tolower(as.character(newlistings$state))),"|",(tolower(as.character(newlistings$Market))),sep="")...again it should be campaignname campaignname <- paste("cat","|",(tolower(as.character(newlistings$BusinessName))),"|",(tolower(as.character(newlistings$BusinessNeed))),sep="")I want to achieve it without using for loop. new to R. any help is appreciated. 解决方案 I thought creating a new dataframe for this was a bit misguided, so I cobbled together a cat-based solution: (it could be easily modified to write to a file.)cat( cat('BusinessId,Campaignname','\n'), apply( Data.csv, 1, function( ln){ cat( ln[1], ',', paste('geo', ln[2],ln[3], sep="|"), "\n") cat( ln[1], ',', paste("cat", ln[4],ln[5], sep="|"), "\n") }))BusinessId,Campaignname 12 , geo|California|Los Angeles 12 , cat|Ray brothers|IT 34 , geo|texas|Dallas 34 , cat|abc|TV 45 , geo|washington|seattle 45 , cat|Microsft|Software 这篇关于如何在csv文件中从不同的函数一个接一个地写数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 05:38