Mohammad Tanvir Ahamed
2016-Oct-02 10:40 UTC
[R] Convert a list with NULL to a dataframe with NA
Hi,
I have a list like below.
OB1 <- structure(list(aa0 = NULL,
aa1 = structure("23403", .Names = "BB10"),
aa2 = structure("54904", .Names = "BB20"),
aa3 = structure("22897", .Names = "BB30"),
aa4 = structure("3751", .Names = "BB40"),
aa5 = NULL,
aa6 = structure("3679", .Names = "BB50"),
aa7 = structure("440193", .Names = "BB60"),
aa8 = structure("23144", .Names = "BB70"),
aa9 = structure("84667", .Names = "BB80"),
aa10 = structure("130540", .Names = "BB90")),
.Names = c("aa0", "aa1", "aa2", "aa3",
"aa4", "aa5", "aa6", "aa7",
"aa8", "aa9", "aa10"))
I am expecting an output like below
OB2 <- structure(list(V1 = structure(c(3L, 5L, 8L, 1L, 4L, 2L, 10L,
7L, 9L, 11L, 6L), .Label = c("aa3", "aa5", "aa0",
"aa4", "aa1", "aa10", "aa7",
"aa2",
"aa8", "aa6", "aa9"), class = "factor"),
id = structure(c(NA, 4L, 8L, 2L, 6L, NA, 5L, 7L, 3L, 9L,
1L), .Label = c("130540", "22897", "23144",
"23403", "3679",
"3751", "440193", "54904", "84667"),
class = "factor"), nam = structure(c(NA,
4L, 8L, 3L, 7L, NA, 6L, 2L, 9L, 5L, 1L), .Label = c("BB90",
"BB60", "BB30", "BB10", "BB80",
"BB50", "BB40",
"BB20", "BB70"), class = "factor")), .Names =
c("V1",
"id", "nam"), row.names = c(NA, -11L), class =
"data.frame")
Problems :
1. Get OB1 to OB2
2. Get OB2 to OB1
I will be great-full if anyone can share idea how to solve the problem .
Thanks in advance !!
Tanvir Ahamed
G?teborg, Sweden | mashranga at yahoo.com
It's fairly straightforward with help from the purrr package:
library(purrr)
map_df(OB1, function(x) {
if (length(x) == 0) {
data.frame(id=NA_character_, nam=NA_character_, stringsAsFactors=FALSE)
} else {
data.frame(id=x[1], nam=names(x), stringsAsFactors=FALSE)
}
}, .id="V1")
On Sun, Oct 2, 2016 at 6:40 AM, Mohammad Tanvir Ahamed via R-help <
r-help at r-project.org> wrote:
> Hi,
>
> I have a list like below.
>
> OB1 <- structure(list(aa0 = NULL,
> aa1 = structure("23403", .Names = "BB10"),
> aa2 = structure("54904", .Names = "BB20"),
> aa3 = structure("22897", .Names = "BB30"),
> aa4 = structure("3751", .Names = "BB40"),
> aa5 = NULL,
> aa6 = structure("3679", .Names = "BB50"),
> aa7 = structure("440193", .Names = "BB60"),
> aa8 = structure("23144", .Names = "BB70"),
> aa9 = structure("84667", .Names = "BB80"),
> aa10 = structure("130540", .Names = "BB90")),
> .Names = c("aa0", "aa1", "aa2",
"aa3",
> "aa4", "aa5", "aa6", "aa7",
> "aa8", "aa9", "aa10"))
>
> I am expecting an output like below
> OB2 <- structure(list(V1 = structure(c(3L, 5L, 8L, 1L, 4L, 2L, 10L,
> 7L, 9L, 11L, 6L), .Label = c("aa3", "aa5",
"aa0",
> "aa4", "aa1", "aa10", "aa7",
"aa2",
> "aa8", "aa6", "aa9"), class =
"factor"),
> id = structure(c(NA, 4L, 8L, 2L, 6L, NA, 5L, 7L, 3L, 9L,
> 1L), .Label = c("130540", "22897", "23144",
"23403", "3679",
> "3751", "440193", "54904",
"84667"), class = "factor"), nam > structure(c(NA,
> 4L, 8L, 3L, 7L, NA, 6L, 2L, 9L, 5L, 1L), .Label = c("BB90",
> "BB60", "BB30", "BB10", "BB80",
"BB50", "BB40",
> "BB20", "BB70"), class = "factor")), .Names =
c("V1",
> "id", "nam"), row.names = c(NA, -11L), class =
"data.frame")
>
> Problems :
> 1. Get OB1 to OB2
> 2. Get OB2 to OB1
>
> I will be great-full if anyone can share idea how to solve the problem .
> Thanks in advance !!
>
>
>
>
> Tanvir Ahamed
> G?teborg, Sweden | mashranga at yahoo.com
>
> ______________________________________________
> R-help at r-project.org 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.
[[alternative HTML version deleted]]