Anna Carter
2010-Feb-19 06:17 UTC
[R] Deleting colmuns with 0's and also writing multple csv files
Dear R helpers, I have two queries. (1) If the dataset contains some variables having all the entries = 0 and while analysing I want to delete those pericular columns, how do acheive this. i.e. dataset1 sr_no var1 var2 var3 var4 var5 1 5 0 3 1 0 2 3 0 2 9 0 3 4 0 4 7 0 4 11 0 1 6 0 In the above dataset, var2 and var3 are all 0's so I don't want to select these columns. It is not that always these two variables will be zeros, so in general how the dataset can be filtered in order to have only non-zero columns. (2) Suppose I have variable no of datasets 'say n = 10'. I wish to write a loop assigning each of these datasets to diffrent csv files e.g. for (i in 1:10) { write.csv(data.frame(dataset[,,i]), 'data_set[i].csv', row.names = FALSE) } The result of this command is generation of a csv file 'data_set[i].csv' containing the last dataset (owing to the wrong command written by me). What I need is creation of say data_set[1].csv, data_set[2].csv, .........data_set[10].csv i.e. 10 different csv files containing 10 different datasets. Thanking you in advance Anna Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! http://downloads.yahoo.com/in/internetexplorer/ [[alternative HTML version deleted]]
K. Elo
2010-Feb-19 06:36 UTC
[R] Deleting colmuns with 0's and also writing multple csv files
Dear Anna, 19.02.2010 08:17, Anna Carter wrote:> (1) If the dataset contains some variables having all the entries = 0 > and while analysing I want to delete those pericular columns, how do > acheive this. i.e.Let's suppose 'df' is your data frame, then: subset(df, select=which(colSums(df)!=0)) should do the work :) HTH, Kimmo
DarioAustralia
2010-Feb-19 06:46 UTC
[R] Deleting colmuns with 0's and also writing multple csv files
Hi Anna, A column with all 0s will have a column sum of zero. So do this : dataset1[, which(colSums(dataSet1 > 0))] If you have a list of data.frames you could do this for(index in 1:10) { write.csv(yourListOfTables[[index]], file = paste("Dataset", index, ".csv", sep = ""), row.names = FALSE) } - Dario. -- View this message in context: http://n4.nabble.com/Deleting-colmuns-with-0-s-and-also-writing-multple-csv-files-tp1561219p1561231.html Sent from the R help mailing list archive at Nabble.com.
RICHARD M. HEIBERGER
2010-Feb-19 07:06 UTC
[R] Deleting colmuns with 0's and also writing multple csv files
> (2)? Suppose I have variable no of datasets 'say n = 10'. I wish to write a loop assigning each of these datasets to diffrent csv files e.g. > > for (i in 1:10) > ?{ > write.csv(data.frame(dataset[,,i]), 'data_set[i].csv', row.names = FALSE) > ?} > > The result of this command is generation of a csv file 'data_set[i].csv' containing the last dataset (owing to the wrong command written by me). > > What I need is creation of say data_set[1].csv, data_set[2].csv,? .........data_set[10].csv i.e. 10 different csv files containing 10 different datasets.Why do you want to create csv datasets? They lose much of the structure that is in the R object. If you are trying to transfer them to somewhere else, then a direct transfer would be a better choice. If the goal is Excel, there are about 5 options. I prefer RExcel because it allows the tightest coordination of the R and the Excel calculations. If the goal is one of the other popular statistical systems, they also have direct connections, often through the foreign package. Rich
Reasonably Related Threads
- plotting least-squares regression against x-axis
- how to use axes=FALSE to get multple plots?
- Specifying multple nodes with mcollective-puppet-agent Host Filters
- multple sqlite connections from single rails application
- "Shrinking" Browse LIST with Multple Ethernet Switches!