Using MAP with a function returning a vector

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Using MAP with a function returning a vector

Veerappa Chetty
Hi Experts,

The following codes work when the return value is a scalar.
 But I like to get the whole vector instead of one element of the vector x
using map  as a data frame instead getting one element of the vector using
of map_dbl.
In other words, I would like a data frame with one row for each iteration.
I would appreciate your help.
Thanks.
Chetty
__________________________________________
library(matlib)
v<-c(0.6,0.3,0.1,0.5,0.3,.2,0.1,0.2,0.8)
markov<-function(v){
nrow<-sqrt(length(v))
B<-matrix(v,nrow=nrow,byrow=TRUE)
A.sub<-t(B)[1:nrow-1,]-diag(nrow)[1:nrow-1,]
A<-rbind(A.sub,c(rep(1,nrow)))
b<-c(rep(0,nrow-1),1)
x<-solve(A,b)
x[2]
}
df<-data.frame(v.1=c(0.05,0.95,0.2,0.8),v.1=c(0.01,0.90,0.4,0.6))
df%>% map(~markov(v,v =.))
map_dbl(df,markov)
________________________________________

Professor of Family Medicine
Boston University
Tel: 617-414-6221, Fax:617-414-3345
emails: [hidden email],[hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.