I have big .csv file. I would like to filter that file into a new table.
For example, I have .csv file as below:
f1 f2 f3 f4 f5 f6 f7 f9 f10 f11
t1 1 0 1 0 1 0 0 0 0 1
t2 1 0 0 0 0 1 1 1 1 1
t3 0 0 0 0 0 0 0 0 0 0
t4 1 0 0 0 1 0 0 0 0 0
t5 0 0 0 0 0 0 0 0 0 0
t6 0 0 0 0 0 0 0 0 0 0
I have a table (as above)
What I want to do is, I want to have new table for each row (meaning that, I will have new table for all rows. e.g, new table for row t1, new table for row t2, new table for row t3 and etc). As in this example, I should have 6 new tables.
To develop new table for each row, there is a condition that need to meet. New table should look at every value in each column. And if the column has a same value with other column in other row (which is value 1), it should be grouped together.
As in this example, new table for t1 will consist t1,t2,t4 because value in column f1 have the same value (which is 1) with the value in f1 for row t2 and t4, also value in f5 is equal with the value in f5 for row t4, and value in f11 is equal with the value in f11 for row t2). So, thats mean, it will check every column. One of the output for should be like this:
f1 f2 f3 f4 f5 f6 f7 f9 f10 f11
t1 1 0 1 0 1 0 0 0 0 1
t2 1 0 0 0 0 1 1 1 1 1
t4 1 0 0 0 1 0 0 0 0 0
As for t2, row t2 should be grouped with t4 because value in f1 in t1 and value f1 in t4 is equal. However, t2 should not consider the earlier row (as in this example, it should not consider t1). Output should be like this:
f1 f2 f3 f4 f5 f6 f7 f9 f10 f11 t2 1 0 0 0 0 1 1 1 1 1 t4 1 0 0 0 1 0 0 0 0 0
Similar to other rows (row t3,t4,t5 and t6), it should look at every value in each column. And if the column has a same value with other column in other row (which is value 1), it should be grouped together.
New table (with row and column header) should be then saved in a new .csv file. The file should be renamed using its row name. for example, as for t1, it should be saved as t1.csv.
This is only a simple example. The proposed solution here will be applied in other big table of data. I need to read abc.csv file. Meaning that, it might be more than 100+ new table will be created (when I used original data).
so far i used this code:
a.files <- grep("^Task_vs_Files", dir(), value=TRUE)
a.files
for(i in 1:length(a.files))
dat <- read.table(file=a.files[i], header=T, sep=",", row.names=1)
(sapply(1:nrow(dat), function(x) if (dat[x,]==1) #check row
(sapply(1:nrow(dat), function(y) if (dat[,y]==1) #check column
{
write.csv( dat[(dat[[x,y]]==1 ) & (1:nrow(dat) >= x) , ] , file = paste("Files_", x) ) #save file based on row names
}
else {NULL} ))
output from a.files:
[1] "Task_vs_Files_Proj.csv" "Task_vs_Files_Whirr.csv"
dataset from one of the file (Task_vs_Files_Proj.csv)
pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
WHIRR-25 1 0 1 0 1 1 1
WHIRR-28 1 0 1 0 0 1 0
WHIRR-55 0 0 1 0 0 0 0
WHIRR-61 0 0 0 0 0 1 0
WHIRR-76 0 0 1 0 0 0 0
WHIRR-87 1 1 1 0 0 1 1
WHIRR-92 1 0 0 1 0 1 1
Appreciate help from the expert!