[R] Maintaining Column names while writing csv file.
Dear R helpers, I have one trivial problem while writing an output file in csv format. I have two dataframes say df1 and df2 which I am reading from two different csv files. df1 has column names as date, r1, r2, r3 while the dataframe df2 has column names as date, 1w, 2w. (the dates in both the date frames are identical also no of elements in each column are equal say = 10). I merge these dataframes as df_new = merge(df1, df2, by = date, all = T) So my new data frame has columns as date, r1, r2, r3, 1w, 2w However, if I try to write this new dataframe as a csv file as write.csv(data.frame(df_new), 'df_new.csv', row.names = FALSE) The file gets written, but when I open the csv file, the column names displayed are as date, r1, r2, r3, X1w, X2w My original output file has about 200 columns so it is not possible to write column names individually. Also, I can't change the column names since I am receiving these files from external source and need to maintain the column names. Kindly guide Regards Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Maintaining Column names while writing csv file.
Hi Vincy, have you checked names(df2) and names(df_new) because by default 'data.frame' checks the column names to ensure that they are syntactically valid variable names and 1w and 2w aren't, so an X is prepended (see ?data.frame and ?make.names). compare tst.fail-data.frame(a=1:3,1a=1:3,1b=1:3) names(tst.fail) #and tst-data.frame(a=1:3,1a=1:3,1b=1:3,check.names = F) names(tst) this works for me: tst2-data.frame(a=1:3,w1=4:6,w3=4:6) write.csv(merge(tst,tst2),file=testr.csv) hth. Am 19.07.2012 08:55, schrieb Vincy Pyne: Dear R helpers, I have one trivial problem while writing an output file in csv format. I have two dataframes say df1 and df2 which I am reading from two different csv files. df1 has column names as date, r1, r2, r3 while the dataframe df2 has column names as date, 1w, 2w. (the dates in both the date frames are identical also no of elements in each column are equal say = 10). I merge these dataframes as df_new = merge(df1, df2, by = date, all = T) So my new data frame has columns as date, r1, r2, r3, 1w, 2w However, if I try to write this new dataframe as a csv file as write.csv(data.frame(df_new), 'df_new.csv', row.names = FALSE) The file gets written, but when I open the csv file, the column names displayed are as date, r1, r2, r3, X1w, X2w My original output file has about 200 columns so it is not possible to write column names individually. Also, I can't change the column names since I am receiving these files from external source and need to maintain the column names. Kindly guide Regards Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Maintaining Column names while writing csv file.
On 19/07/12 18:55, Vincy Pyne wrote: Dear R helpers, I have one trivial problem while writing an output file in csv format. I have two dataframes say df1 and df2 which I am reading from two different csv files. df1 has column names as date, r1, r2, r3 while the dataframe df2 has column names as date, 1w, 2w. (the dates in both the date frames are identical also no of elements in each column are equal say = 10). I merge these dataframes as df_new = merge(df1, df2, by = date, all = T) So my new data frame has columns as date, r1, r2, r3, 1w, 2w However, if I try to write this new dataframe as a csv file as write.csv(data.frame(df_new), 'df_new.csv', row.names = FALSE) The file gets written, but when I open the csv file, the column names displayed are as date, r1, r2, r3, X1w, X2w My original output file has about 200 columns so it is not possible to write column names individually. Also, I can't change the column names since I am receiving these files from external source and need to maintain the column names. Just omit the unnecessary and redundant call to data.frame() inside your call to write.csv(). I.e. just do: write.csv(df_new, 'df_new.csv', row.names = FALSE) It is that call to data.frame() that is forcing legal names on you, *not* the call to write.csv(). cheers, Rolf Turner __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Maintaining Column names while writing csv file.
HI, Possibly check.names=FALSE issue. Try this: dat1-read.table(text= 2.5a 3.6b 7.1c 7.9d 100 3 4 2 3 200 3.1 4 3 3 300 2.2 3.3 2 4 ,sep=,header=TRUE) dat1 #You can get rid of those X by either using check.names=FALSE while reading the data #with check.names=FALSE dat1-read.table(text= 2.5a 3.6b 7.1c 7.9d 100 3 4 2 3 200 3.1 4 3 3 300 2.2 3.3 2 4 ,sep=,header=TRUE,check.names=FALSE) dat1 #or before write.csv() colnames(dat1)-gsub(^[X](.*),\\1,colnames(dat1)) A.K. - Original Message - From: Vincy Pyne vincy_p...@yahoo.ca To: r-help@r-project.org Cc: Sent: Thursday, July 19, 2012 2:55 AM Subject: [R] Maintaining Column names while writing csv file. Dear R helpers, I have one trivial problem while writing an output file in csv format. I have two dataframes say df1 and df2 which I am reading from two different csv files. df1 has column names as date, r1, r2, r3 while the dataframe df2 has column names as date, 1w, 2w. (the dates in both the date frames are identical also no of elements in each column are equal say = 10). I merge these dataframes as df_new = merge(df1, df2, by = date, all = T) So my new data frame has columns as date, r1, r2, r3, 1w, 2w However, if I try to write this new dataframe as a csv file as write.csv(data.frame(df_new), 'df_new.csv', row.names = FALSE) The file gets written, but when I open the csv file, the column names displayed are as date, r1, r2, r3, X1w, X2w My original output file has about 200 columns so it is not possible to write column names individually. Also, I can't change the column names since I am receiving these files from external source and need to maintain the column names. Kindly guide Regards Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.