I have this script:
dat <- read.csv(file="Task_vs_Files_Whirr2.csv", header=T, sep=",",
row.names=1)
(sapply(1:nrow(dat), function(x) {
if (dat[x,2]==1) {
write.csv(dat[ (dat[[2]]==1 ) & (1:nrow(dat) >= x) , ] ,
file = paste("fil_", x, ".csv") )
} else {
NULL
}))
but the script return NULL value, as below:
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
[[5]]
NULL
[[6]]
NULL
[[7]]
NULL
Here are other details:
> str(dat)
'data.frame': 7 obs. of 7 variables:
$ pom.xml. : int 1 1 0 0 0 1 1
$ ZooKeeper.java : int 0 0 0 0 0 1 0
$ HBase.java : int 1 1 1 0 1 1 0
$ Hadoop.java. : int 0 0 0 0 0 0 1
$ BasicServer.java.: int 1 0 0 0 0 0 0
$ Abstract.java. : int 1 1 0 1 0 1 1
$ HBaseRegion.java : int 1 0 0 0 0 1 1
> dput(dat)
structure(list(pom.xml. = c(1L, 1L, 0L, 0L, 0L, 1L, 1L), ZooKeeper.java = c(0L,
0L, 0L, 0L, 0L, 1L, 0L), HBase.java = c(1L, 1L, 1L, 0L, 1L, 1L,
0L), Hadoop.java. = c(0L, 0L, 0L, 0L, 0L, 0L, 1L), BasicServer.java. = c(1L,
0L, 0L, 0L, 0L, 0L, 0L), Abstract.java. = c(1L, 1L, 0L, 1L, 0L,
1L, 1L), HBaseRegion.java = c(1L, 0L, 0L, 0L, 0L, 1L, 1L)), .Names = c("pom.xml.",
"ZooKeeper.java", "HBase.java", "Hadoop.java.", "BasicServer.java.",
"Abstract.java.", "HBaseRegion.java"), class = "data.frame", row.names = c("WHIRR-25",
"WHIRR-28", "WHIRR-55", "WHIRR-61", "WHIRR-76", "WHIRR-87", "WHIRR-92"
))
> sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Trying to salvage something…. (see my comment above as to why NULL is being returned)
If you are only interested in those data where
dat[,2]==1, you might as well usewhichto find thoserowsnow, do you really want separate files containing the same data, repeated without the header row each time? That is what your code is doing now.
writing a
forloop because it is far simpler to understandWith your current data this will create a file
fil_6.csvcontaining the 6th row of your data frame (which is the only row wherex[,2]==1.Whether this script will scale to a bigger data set is unclear, because it is unclear what you want to do.