r - Looping Sum for Word-filled function -
r - Looping Sum for Word-filled function -
i've set list:
dow30 <- c("mmm","axp","t","ba","cat","cvx","csco","dd","xom","ge","gs","hd","intc","ibm","jnj","jpm", "mcd","mrk","msft","nke","pfe","pg","ko","trv","utx","unh","vz","v","wmt","dis")
i want each of items (individually, in quotes) looped through function, sumprofitonly, , (numeric) returns each item summed together.
this longhand method gets me final reply i'm looking ...
print(sumprofitonly("dis") + sumprofitonly("mmm") + sumprofitonly("axp") + sumprofitonly("t") + sumprofitonly("ba") + sumprofitonly("cat") + sumprofitonly("cvx") + sumprofitonly("csco") + sumprofitonly("dd") + sumprofitonly("xom") + sumprofitonly("ge") + sumprofitonly("gs") + sumprofitonly("hd") + sumprofitonly("intc") + sumprofitonly("ibm") + sumprofitonly("jnj") + sumprofitonly("jpm") + sumprofitonly("mcd") + sumprofitonly("mrk") + sumprofitonly("msft") + sumprofitonly("nke") + sumprofitonly("pfe") + sumprofitonly("pg") + sumprofitonly("ko") + sumprofitonly("trv") + sumprofitonly("utx") + sumprofitonly("unh") + sumprofitonly("vz") + sumprofitonly("v") + sumprofitonly("wmt"))
... i'm trying create quicker function work.
my (failed) efforts:
1) based on this related issue --
rate <- function(n) { result <- lapply(n, function(i) data.frame(n = i, calculated = as.vector(sumprofitonly(i)))) return(do.call(rbind, result)) } print(rate(dow30))
this yields each individual result in dataframe (along original text ticker, no quotes). looks this:
n calculated 1 mmm 4.01 2 axp 3.31 3 t -1.05 4 ba -0.77 5 cat 3.40 6 cvx 1.90 7 csco 2.46 8 dd 3.95 9 xom 2.03 etc., etc., etc.
i've tried shove sum around things, either ignores sum function, or comes error, after effort of altering print:
print(sum(rate(dow30)), na.rm = false)
giving error:
error in fun(x[[1l]], ...) : defined on info frame numeric variables
2) based on answered issue:
for (n in dow30) { printer[n] = print(sumprofitonly(n)) }
yielding this:
[1] 4.01 [1] 3.31 [1] -1.05 [1] -0.77 [1] 3.4 [1] 1.9 [1] 2.46 [1] 3.95 [1] 2.03 etc., etc., etc.,
here, 1 time again, adding sum within , out of function not successful, returns "sum" of last value in list.
r function loops data.frame sum
Comments
Post a Comment