使用r中的多个文件获取R输出中的统计数据和绘图

时间:2017-02-09 00:52:26

标签: r

我尝试使用此代码来获取r中多个文件的统计信息和绘图。使用此代码,我可以获得数据框的图和数据结构(不包括图)。我在代码下面加了R输出。如何操作此代码以获取我的anaylze函数中的其他信息?

getwd()
options(max.print=100000)
##################
#DATA EXPLORATION#
##################
#Here we are just stating the type and name of the data set, and that
# attach(filenames)
#Function for summary statistics
anaylze<- function(filename){
  YC<-read.csv(file = filename, header = TRUE, sep=",", row.names="number",na.strings="")
  names(YC)
  str(YC) #to check breakdown of structure of a data frame
  summary(YC) #summary stat
  pairs(YC) #basic scatterplot matrix
  cor(YC) #determines correlations b/w 2 variables
  par(mar=rep(2,4))
  boxplot(YC) #indicates if variables differ in magnitiude and that they have different variances
  library(MVN)
  uniNorm(YC, type="SW", desc=TRUE) #gets the shapiro-wilks test of univariate normality along w/ descriptive stats like skewness and  kurtosis
  par(mar=rep(2,4))
  uniPlot(YC, "histogram") #visulization step
  result<-mardiaTest(YC, qqplot=T)#to get all plot within the same frame
  result 
}
#to see the result of the MV normality test for the soil data
filenames <- list.files(path = "/Users/jblamkey/OneDrive/School/R/R Workspace", pattern = "CodaBrundage", full.names=TRUE)
filenames <- filenames[1:5]
for (YC in filenames) {
  print(YC)
  anaylze(YC)
}

###R OUTPUT###

> getwd()
[1] "/Users/jblamkey/OneDrive/School/R/R Workspace"
> options(max.print=100000)
> ##################
> #DATA EXPLORATION#
> ##################
> #Here we are just stating the type and name of the data set, and that
> # attach(filenames)
> #Function for summary statistics
> anaylze<- function(filename){
+   YC<-read.csv(file = filename, header = TRUE, sep=",", row.names="number",na.strings="")
+   names(YC)
+   str(YC) #to check breakdown of structure of a data frame
+   summary(YC) #summary stat
+   pairs(YC) #basic scatterplot matrix
+   cor(YC) #determines correlations b/w 2 variables
+   par(mar=rep(2,4))
+   boxplot(YC) #indicates if variables differ in magnitiude and that they have different variances
+   library(MVN)
+   uniNorm(YC, type="SW", desc=TRUE) #gets the shapiro-wilks test of univariate normality along w/ descriptive stats like skewness and  kurtosis
+   par(mar=rep(2,4))
+   uniPlot(YC, "histogram") #visulization step
+   result<-mardiaTest(YC, qqplot=T)#to get all plot within the same frame
+   result 
+ }
> #to see the result of the MV normality test for the soil data
> filenames <- list.files(path = "/Users/jblamkey/OneDrive/School/R/R Workspace", pattern = "CodaBrundage", full.names=TRUE)
> filenames <- filenames[1:5]
> for (YC in filenames) {
+   print(YC)
+   anaylze(YC)
+ }
[1] "/Users/jblamkey/OneDrive/School/R/R Workspace/2016_CodaBrundage-Entry-001.csv"
'data.frame':   489 obs. of  14 variables:
 $ area_mm2      : num  17.22 10.49 9.43 16.75 18.63 ...
 $ perimeter_mm  : num  21 16.6 14.4 19.3 20.3 ...
 $ majellipse_mm : num  6.82 5.44 3.77 5.95 6.28 ...
 $ minellipse_mm : num  3.22 2.47 3.29 3.6 3.78 ...
 $ grain..ch1    : num  216 198 215 211 213 ...
 $ grain..ch2    : num  191 171 192 181 187 ...
 $ grain..ch3    : num  132 113 133 119 132 ...
 $ nonCrease..ch1: num  216 198 215 211 213 ...
 $ nonCrease..ch2: num  189 171 192 181 187 ...
 $ nonCrease..ch3: num  130 113 133 119 132 ...
 $ crease..ch1   : num  206 0 0 0 0 ...
 $ crease..ch2   : num  172 0 0 0 0 ...
 $ crease..ch3   : num  111 0 0 0 0 ...
 $ CreaseUp      : int  1 0 0 0 0 1 0 1 0 0 ...
[1] "/Users/jblamkey/OneDrive/School/R/R Workspace/2016_CodaBrundage-Entry-002.csv"
'data.frame':   420 obs. of  14 variables:
 $ area_mm2      : num  15.9 18.3 13.3 12.7 16.4 ...
 $ perimeter_mm  : num  19.3 21.6 17.6 18.6 19.1 ...
 $ majellipse_mm : num  6.31 6.95 5.72 6.18 6.04 ...
 $ minellipse_mm : num  3.22 3.36 2.97 2.62 3.47 ...
 $ grain..ch1    : num  211 226 215 226 219 ...
 $ grain..ch2    : num  180 195 188 204 190 ...
 $ grain..ch3    : num  126 137 126 154 133 ...
 $ nonCrease..ch1: num  211 226 215 220 219 ...
 $ nonCrease..ch2: num  180 195 188 198 190 ...
 $ nonCrease..ch3: num  126 137 126 149 133 ...
 $ crease..ch1   : num  0 0 0 194 0 ...
 $ crease..ch2   : num  0 0 0 173 0 ...
 $ crease..ch3   : num  0 0 0 127 0 ...
 $ CreaseUp      : int  0 0 0 1 0 1 0 0 0 1 ...
[1] "/Users/jblamkey/OneDrive/School/R/R Workspace/2016_CodaBrundage-Entry-003.csv"
'data.frame':   345 obs. of  14 variables:
 $ area_mm2      : num  14.7 15.3 19.6 19.2 19.5 ...
 $ perimeter_mm  : num  19.5 19 20.9 21.8 22.2 ...
 $ majellipse_mm : num  6.52 5.98 6.75 7.36 7.35 ...
 $ minellipse_mm : num  2.88 3.28 3.7 3.32 3.38 ...
 $ grain..ch1    : num  222 229 226 226 226 ...
 $ grain..ch2    : num  197 204 200 201 202 ...
 $ grain..ch3    : num  138 145 141 145 150 ...
 $ nonCrease..ch1: num  222 229 224 226 226 ...
 $ nonCrease..ch2: num  197 204 197 200 201 ...
 $ nonCrease..ch3: num  138 145 138 144 148 ...
 $ crease..ch1   : num  0 0 202 221 211 ...
 $ crease..ch2   : num  0 0 171 191 181 ...
 $ crease..ch3   : num  0 0 114 134 126 ...
 $ CreaseUp      : int  0 0 1 1 1 1 1 1 0 1 ...
[1] "/Users/jblamkey/OneDrive/School/R/R Workspace/2016_CodaBrundage-Entry-004.csv"
'data.frame':   461 obs. of  14 variables:
 $ area_mm2      : num  16.2 15.5 16.5 16.3 16.8 ...
 $ perimeter_mm  : num  20.2 20.7 19.5 19.8 20.2 ...
 $ majellipse_mm : num  6.39 6.61 5.99 6.59 6.52 ...
 $ minellipse_mm : num  3.27 3.04 3.52 3.16 3.29 ...
 $ grain..ch1    : num  234 211 196 218 218 ...
 $ grain..ch2    : num  205 178 167 191 190 ...
 $ grain..ch3    : num  138 118 116 135 132 ...
 $ nonCrease..ch1: num  228 211 199 218 218 ...
 $ nonCrease..ch2: num  201 178 168 191 190 ...
 $ nonCrease..ch3: num  136 118 117 135 132 ...
 $ crease..ch1   : num  177 0 197 0 0 ...
 $ crease..ch2   : num  160 0 161 0 0 ...
 $ crease..ch3   : num  114 0 109 0 0 ...
 $ CreaseUp      : int  1 0 1 0 0 1 1 0 0 1 ...
[1] "/Users/jblamkey/OneDrive/School/R/R Workspace/2016_CodaBrundage-Entry-005.csv"
'data.frame':   439 obs. of  14 variables:
 $ area_mm2      : num  17.2 13.1 15.2 20.1 11.6 ...
 $ perimeter_mm  : num  19.3 19.6 18.5 21.8 17.7 ...
 $ majellipse_mm : num  6.63 6.39 5.87 6.9 5.93 ...
 $ minellipse_mm : num  3.32 2.64 3.31 3.73 2.52 ...
 $ grain..ch1    : num  198 207 228 210 212 ...
 $ grain..ch2    : num  170 179 201 181 186 ...
 $ grain..ch3    : num  120 122 144 122 136 ...
 $ nonCrease..ch1: num  197 207 228 210 209 ...
 $ nonCrease..ch2: num  169 179 201 181 182 ...
 $ nonCrease..ch3: num  120 123 144 122 133 ...
 $ crease..ch1   : num  183 197 0 0 197 ...
 $ crease..ch2   : num  153 169 0 0 166 ...
 $ crease..ch3   : num  106 116 0 0 118 ...
 $ CreaseUp      : int  1 1 0 0 1 0 0 1 0 1 ...

1 个答案:

答案 0 :(得分:0)

将每个统计信息添加到analyze中的列表中,然后返回该列表。这是一个小例子,因为我懒得在没有文件的情况下重现你的例子。

analyze <- function (testdata) {
  results=list()
  results$mean = mean(testdata)
  plot(testdata)
  results$plot = recordPlot()
  return(results)
}

datasets <- list(c(1,5,10), c(10,15,20),c(30,40,50))

all_results = list()
for (dataset in datasets) {
  # append new results to list
  all_results[[length(all_results) + 1]] <- analyze(dataset)
}


all_results[[1]]$mean
all_results[[2]]$mean
all_results[[2]]$plot

# loop over all_results if you want
for(results in all_results) {
  print(results$mean)
}