r - How to easily manipulate the data.frame in the list let them have same pattern? -
i implement function return list of data.frame order of index vector in data.frame different. however, intend manipulate list let second, third data.frame has same pattern first data.frame in list. because result in list, want manipulate list let data.frame objects has same pattern. how can make happen ? knows useful trick of manipulating list ? in advance.
the output of custom function :
dflist <- list( df_1 = data.frame(hola = c(1,2,3), ciao=c(na,1,2), bonjour=c(1,1,2)), df_2 = data.frame(ciao = c(1,2,3), hola=c(2,3,na), bonjour=c(1,2,4)), df_3 = data.frame(bonjour = c(1,2,3,4), hola=c(2,3,na,na), ciao=c(1,2,na,3)) )
i intend make second, third data.frame has same pattern first data.frame in list. how can manipulate data.frame has same pattern in list?
desired output be:
$df_1 hola ciao bonjour 1 1 na 1 2 2 1 1 3 3 2 2 $df_2 hola ciao bonjour 1 2 1 1 2 3 2 2 3 na 3 4 $df_3 hola ciao bonjour 1 2 1 1 2 3 2 2 3 na na 3 4 na 3 4
i know easier if data.frame not in list, using names() let them have same pattern. point data.frame in list output now, intend manipulate list nicely instead of access each data.frame 3 time , use names() function. how can make happen ? how can achieve desired output ? how can more dynamic solution on that? lot :)
we can use
lapply(dflist, function(x) x[names(dflist[[1]])])
Comments
Post a Comment