gpt4 book ai didi

读入具有两行标题的文件

转载 作者:行者123 更新时间:2023-12-04 02:21:53 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Reading two-line headers in R

(5 个回答)


8年前关闭。




我一直在使用 SurveyGizmo,它可以将数据导出为 CSV 文件,但可惜它有两行标题。第一行指定问题,第二行包含受访者本可以勾选的可能答案。这在数据读写世界中似乎非常异常,但在调查世界中似乎很正常。如何将这样的文件读入 R?
SurveyGizmo 曾经有一个“旧”的导出格式,将所有内容放在一行中,但似乎公司不再支持它了。在一项简单的调查中,帮助我的实习生能够使用以下代码解决问题。然而,随着调查时间更长,问题更多,问题更长(因此标题更长),我们上面的蛮力方法不起作用。

#Read csv file with two rows of headers
#Append the second row to the first row
df <-read.csv(csvfile,skip=1,stringsAsFactors=FALSE) #Read csv without any header
hl=readLines(csvfile, 2) #Read the two header lines as char strings
hl=strsplit(hl,',') #Split headers up by commas
colnames(df)=sub('_$','',paste(hl[[1]],hl[[2]],sep="")) #join second row to first row
最后,我想要一个带有列标题的数据框,然后我将与来自后续调查的另一个数据框合并。
以下是具有两个标题行的 CSV 文件示例。第三行也是最后一行是第一行数据(不是真实信息)。
"","","","","","","","","","Inclusion Criteria I or my child is a patient with recurrent respiratory papillomatosis (RRP)How do you know that you or your child has RRP? Please check whatever is true.","","","Exclusion Criteria Do any of the following apply? Please put a check next to any condition that is present.In the unlikely event that one of the following conditions apply, then unfortunately we cannot enroll you in this study. You could stop or you could carry on telling us about yourself, whichever you prefer. ","","Confused or have questions?If you are confused about any items or if you want us to clarify something then here is the place that you can express yourself freely. Also, you can call us at (412) 567-7870 or at (888) 887-7729.You are encouraged to review the consent form. You do not have to sign it now but you will need to do so once we enroll you. ","Please tell us who you are - referring to you, the person completing the form. Different people feel differently about their privacy and about how they are contacted. We will do our utmost to protect your privacy. Please do not give us your e-mail address if you do not want us to use it. Remember that e-mail should be private but is not always so. The safest way to think about it is as if e-mail was similar to a post card. Please do not give us a telephone number you do not want us to contact you on.","","","","","","","","","","","Who are you? Are you the patient or a parent or someone else?","When was the person with RRP born?Enter the date as MM/DD/YYYY","Approximately when was RRP diagnosed? This can be very approximate. If you do not remember the date then please put down your best guess. We will use it to work out how old the patient was when he or she was diagnosed. Enter the date as MM/DD/YYYY.","Has the patient with RRP ever received Gardasil? Gardasil is a vaccine against HPV 6, 11, 16 and 18 that was approved by the Food and Drug Administration (FDA) for use in females to prevent gynecologic diseases. ","Please ignore this question. It is for our internal tracking. Are you?","gender","race","Has there been human contact? By e-mail or by telephone or by anything in which we discussed informed consent","What is the subject number?","Merck Research Laboratory Accession Number?","Second Merck  Accession Number?","FedEx Tracking Number","Date Shipped Out","Date EMSI Notified"
"Response ID","RespondantKey","Edit Link","IP","Date Started","Date Finished","Status","Linked From","Comments","histopathconfirm","surgeonseaid","other","cancer","none","","First Name","Last Name","Street Address","Apt/Suite/Office","City","State","Postal Code","Country","Email Address","Phone Number","Mobile Phone","","","","","","","","","","","","","",""
"6990181","4099941","http://s-gtzd7-14166.sgizmo.com/?edit=6770181&cc=e246ecb7095b983xxxxx7ec0a9","1991.157.178.134","2009-04-30 07:57:24","2009-04-15 14:56:01","Submitted","","Spoke to her Thursday, 20 Apr 2009 20:26. No questions ready to go.09/11/2009 consent mailed..mrs accession number 304074333811wp, 01wp SFJB06123 Fedex tracking 865888887357 sent Tues April 29; called her Thurs, 10 May 2009 20:21 she will sign slip","histopathconfirm","surgeonseaid","","","none","","Jane","Doe","23 Hastings Rd","29th floor","Oranje","ny","27935","USA","mystry@gmail.com","728-850-7252","626-922-2239","Patient","02/21/1965","01/01/1976","No","Key Person","","","Yes","SFJB06123","304033385811wp","303334485801wp","865333807357","4/11/2007","4/11/2007"

最佳答案

为什么不只有 读取.csv 读入第一个标题行(这是我理解你的问题的实际标题)然后跳过下一行:

read.csv(file, header=T, skip=1)

或者,如果第二个标题行以一个特殊字符(在您的数据中找不到)开头,那么您可以通过将该行开始的字符作为值传递给参数 来将该行指定为注释行。评论.char (例如,如果该行以“#”开头,则为)::
read.csv(file, header=T, comment.char="#")

关于读入具有两行标题的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2293131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com