Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何使用之前为几百个类似数据集编写的命令生成相同类型的图形?_File_R_Graph_Command - Fatal编程技术网

File 如何使用之前为几百个类似数据集编写的命令生成相同类型的图形?

File 如何使用之前为几百个类似数据集编写的命令生成相同类型的图形?,file,r,graph,command,File,R,Graph,Command,我在一个数据文件中有几百个数据集,我需要首先获取每个数据集的子集,我已经编写了生成图形和csv文件的命令。然后我想为其余的数据集生成相同类型的图形和csv文件。我想知道R中是否有我可以使用的命令 更具体地说,我为一个特定的子集编写了如下命令,然后我需要对其余的数据子集做同样的事情,我唯一需要更改的是修改子集名称,例如,将“七”更改为“八”,将“七”更改为“八”,等等,R里有没有命令可以帮我?(所以我不需要重复我自己修改名字,复制和粘贴相同的东西到R中)非常感谢 alldata <- rea

我在一个数据文件中有几百个数据集,我需要首先获取每个数据集的子集,我已经编写了生成图形和csv文件的命令。然后我想为其余的数据集生成相同类型的图形和csv文件。我想知道R中是否有我可以使用的命令

更具体地说,我为一个特定的子集编写了如下命令,然后我需要对其余的数据子集做同样的事情,我唯一需要更改的是修改子集名称,例如,将“七”更改为“八”,将“七”更改为“八”,等等,R里有没有命令可以帮我?(所以我不需要重复我自己修改名字,复制和粘贴相同的东西到R中)非常感谢

alldata <- read.csv(file="file.csv",header=T,sep=",")

seven<- subset(alldata, aserno==7, select=c(I,C,D))  # aserno==7, so I need to change 7 into different numbers included in the data file

sevenout <- subset(seven, I=="a" & D>0, select=c(I,C,D))

f <- function(sevenoutf) nrow(sevenoutf)

sevennumber <- ddply(sevenout,.(C), f)

colnames(sevennumber)[2] <- "N"

sevenout$N <- sevennumber$N [match(sevenout$C, sevennumber$C)]

sevenout=data.frame(sevenout,"time"=c(1:nrow(sevenout)))

plot(sevenout$time, sevenout$N, type="n")

lines(sevenout$time,sevenout$N)           # the result that I need

write.csv(sevenout, "sevenM.csv", row.names=FALSE)        # the result that I need

alldata尝试编写一个在内部调用子函数的包装函数。然后根据需要使用apply()函数(tapply、sapply等)通过列表将变量名传递给函数

MyWrapperFunction <- function( infile) {

                      a <- read.csv(file = infile)
                      a1 <- subset(a, inout==2, select=c(A,C,D))
                      f.sum <- function(a1f) sum(a1f$D)
                      atd <- ddply(a1, .(a1$C), f.sum)
                      colnames(atd)[2] <- "td"

                      f.nrow <- function(a1f) nrow(a1f)
                      aC <- ddply(a1,.(a1$C), f.nrow)
                      colnames(aC)[2] <- "number"

                      a_A <- merge(atd, aC, by="a1$C")
                      myplot <- plot(a_A$number, a_A$td)  # the result I need
                      # save as your desired image file, png, pdf, etc...
                      # e.g. pdf( myplot, file = paste( infile, "_plot.pdf, sep = "") )

                      # the result I need
                     write.csv(a_A, 
                              file = paste( infile, "_output.csv, sep = ""),
                              row.names=FALSE) }
并对write.csv执行与上面所示相同的操作


祝你好运

你可以使用for循环来完成类似的事情,因为你想清楚地看到你在做什么,并在一个步骤中完成所有的迭代。如果文件名在同一目录中,而该目录中没有其他内容,则还可以读入文件名列表。例如:

setwd("/my_docs/my_project_data/") # where all your data files are

my.files <- list.files()

setwd("/my_docs/my_project_graphs/") # somewhere to save your graphs

for(i in 1:length(my.files))
   {
   temp.dat <- read.csv(my.files[i])
   YOUR FUNCTION

   pdf(paste(Sys.Date(),"_",my.files[i],"_graph.pdf", sep="")) # naming the pdf that will be written out
   plot(temp.dat$number, temp.dat$td, main=my.files[i])
   dev.off()
   write.csv(temp.dat, paste(Sys.Date(),"_",my.files[i],"_new_data.pdf", sep=""), row.names=FALSE)
   }
setwd(“/my_docs/my_project_data/”)#所有数据文件都在其中

my.files请在单独的行中重新编写问题中的代码,以便清楚发生了什么,请使用4个空格,以便将它们突出显示为代码。Chris,您能再扩展一点吗?我认为写一个包装器函数是解决这个问题的方法,OP可能需要向正确的方向再推进一次。我不确定在这种情况下使用
apply()
函数是否正确。但是这应该会让OP走上一段路。嘿,克里斯,非常感谢你的帮助:)在我编辑我的问题时,我发现文件有错误,所以现在我只有一个数据文件,其中包含所有数据,所以我需要对它们进行子集。您能帮助我如何使用一个命令一次将所有文件子集化吗?或者你能告诉我当所有数据都在一个文件中时该怎么做(我在问题中为一个子集写了命令。)再次感谢!哦,通过你的修改,你的问题现在变得容易多了。只需使用问题中的新代码编写一个通用函数,并通过变量将subsetting命令发送到
subset()
谢谢!你能把你的答案扩大一点吗?例如,如何编写泛型函数??我不太熟悉R。。。此外,数据文件中的数据子集没有按顺序命名,名称类似于7,然后跳到103,然后跳到400,如何列出子集名称?再次感谢你的帮助!非常感谢你!但我很抱歉,我刚刚发现几百个文件都有错误,我得到的新数据文件包含了所有数据,这意味着我需要先获取子集。所以我想知道如何列出子集并完成命令?相似吗?我已经编辑了我的问题,并包含了新的命令。请帮帮我,谢谢:)
anotherWrapperFunction <- function( data, subset.critera,...) 
data.subset <- subset( data, aserno == subset.critera, #etc...)

data.subset.subset <- subset( data.subset, #etc...) 
f <- function(sevenoutf) nrow(sevenoutf) 
data.subset.subset.ddply <- ddply( data.subset.subset, #etc...)

colnames(data.subset.subset.ddply)[2] <- "N"
# save as your desired image file, png, pdf, etc...
# using the subset criteria in the file name so you can ID the plot
# e.g. pdf( myplot, file = paste( "subset", subset.critera, "_plot.pdf, sep = "") )
setwd("/my_docs/my_project_data/") # where all your data files are

my.files <- list.files()

setwd("/my_docs/my_project_graphs/") # somewhere to save your graphs

for(i in 1:length(my.files))
   {
   temp.dat <- read.csv(my.files[i])
   YOUR FUNCTION

   pdf(paste(Sys.Date(),"_",my.files[i],"_graph.pdf", sep="")) # naming the pdf that will be written out
   plot(temp.dat$number, temp.dat$td, main=my.files[i])
   dev.off()
   write.csv(temp.dat, paste(Sys.Date(),"_",my.files[i],"_new_data.pdf", sep=""), row.names=FALSE)
   }