Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

Generate a Function-Parameter in R with paste-function

How can i dynamically generate a function-paramater in R? I will use the paste0 function for different parameters for the line-function:

lines(paste0(gdaxisymbol[i],"$",gdaxisymbol[i],".Adjusted"))

It ends with an warning: In xy.coords(x, y) : NAs durch Umwandlung erzeugt

thx

Hi, thx for your answer and sorry for the unclear question Here is my full code:

library(XML)
library(RCurl)

s <- getURL("https://de.finance.yahoo.com/quote/^GDAXI/components")
t=readHTMLTable(s)
gdaxi = t[["NULL"]]
gdaxi = gdaxi[,-(3:6) ]
gdaxisymbol <- gdaxi$Symbol

getSymbols(gdaxisymbol,from="2015-12-31",to="2021-01-31", auto.assign = TRUE)
plot(ADS.DE$ADS.DE.Adjusted)
for(i in 1:30)
{
  
  lines(paste0(gdaxisymbol[i],"$",gdaxisymbol[i],".Adjusted"))
}

I would like to print all Adujsted share prices in one Plot. But if i execute the loop i become the error above. How can i give the correct parameter dynamicaly to the lines function? If i print one line with

lines(ALV.DE$ALV.DE.Adjusted)

it works fine.

thx

question from:https://stackoverflow.com/questions/66047233/generate-a-function-parameter-in-r-with-paste-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This questions isn't perfectly clear. Not sure whether the problem is with the lines function or something else. But I'll answer the question regarding how to dynamically pass parameters for a function.

If you want to dynamically pass parameters for a function and send them into paste0, the easiest way to do it is to pass the parameters in on a list or vector. Here is an example:

# Define function 
paste_special <- function(inputs) {
  
 ret <- paste0(inputs, collapse = "") 
  
 return(ret)
}

# Create sample data
myinputs <- c("A", "$", "B", ".adjusted")

# Call function and view results
paste_special(myinputs)
# [1] "A$B.adjusted"

Since the list or vector is flexible, and can take any number of elements, then your function can also take any number of elements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...