[R] Using MAP with a function returning a vector

Veerappa Chetty chettyvk @ending from gm@il@com
Mon Jun 11 20:26:32 CEST 2018


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: chettyvk using gmail.com,vchetty using bu.edu

	[[alternative HTML version deleted]]



More information about the R-help mailing list