The set of sets in R

Earlier, I asked about this question , which was useful when building a function. I want to try to build twenty functions on the same axes to illustrate how a function changes between two ranges. I successfully did this using individually defined functions, but I wanted to do it with a loop.

I tried to do the following:

## add ggplot2
library(ggplot2)
library(lattice)

# Declare local variables
inPath = "D:/R_Analysis/"
inFile = "sample.txt"

outPath = "D:/R_Analysis/"
outFile = "processed_sample.txt"

pdfOutPath = "D:/R_Analysis/"
pdfOutFile = "processed_sample.pdf"

# Declare Chart values
y_label = "x-axis"
x_label = "y-axis"
chart_title = "..." 

#####################################################################
## Read in data;  
analysis <- 
read.table(paste(inPath, inFile, sep=""), header=TRUE, sep=",", 
na.strings="NA",  dec=".", strip.white=TRUE)

# Setup pdf
pdf(paste(pdfOutPath, pdfOutFile, sep=""),height=6,width=9)

# make plot object    
p <- qplot(
data = data.frame(x = x, y = y), x, y, xlab = x_label, ylab = y_label, 
enter code herexlim = x_range, main = chart_title  )

# make empty function
eq_dummy = function(x){ 0 }
d = stat_function(fun = eq_dummy)

##############
# LOOP #######

for(i in 1 : 21){                                            

        # Specify Variables
        intercept = analysis[i,2]
        slope = analysis[i,3]    

        # Define Curve    
        eq <- function(x) { slope * log(x) + intercept }

        # Make plot object            
        composite <- stat_function(fun=eq)        
        composite = composite + d       

}

print(p + composite)  

# Show warnings
warnings()

# close the PDF file
dev.off() 

Any suggestions for improving the syntax or programming structure would be appreciated. Thank.

+3
source share
2 answers

There is a nice feature file.paththat allows you to create file paths independently. You can use it in your code like:

inPath = file.path("D:","R_Analysis")
inFile = "sample.txt"
outPath = file.path("D:","R_Analysis")
outFile = "processed_sample.txt"
pdfOutPath = file.path("D:","R_Analysis")
pdfOutFile = "processed_sample.pdf"

read.table(file.path(inPath, inFile))
pdf(file.path(pdfOutPath, pdfOutFile))

" " ( ), , .

- , .

pdf(file.path(pdfOutPath, pdfOutFile),height=6,width=9)
print(p + composite)  
dev.off()

, , .

+3

. , <- =; . Google Hadley Wickham.

read.table sep=',' header=TRUE, , read.csv.

, script. , , . , (, ).

R Inferno R.

+1

Source: https://habr.com/ru/post/1731130/


All Articles