CodeMaster
CodeMaster

Reputation: 449

Unnest data frames within a data frame - R

I have a dataframe with 4 columns with the last column as a dataframe in itself as shown below.

enter image description here

I would like to unnest this data frame as a new dataframe with 3 colums:

tagname, localDateTime, Value

I could use a simple statement as

DF_unnest <- unnest(DF, dataList)
outTagValues = DT_unnest%>%
            select(tagname,localDateTime,value)    

The above statement is concise and works like a charm but I would like to know if there is another way that does not involve the function "unnest" . I am having an issue in Spotfire where we can't upgrade to the latest version of tidyr where the above statement works but fails in previous versions of tidyr.

Here is dput:

structure(list(entityInstanceID = c(NA, NA), attributeName = c(NA, 
NA), tagname = c("LU_HIST_SVR.C874_1_DPTSlope", "LU_HIST_SVR.C874_1_DPTTransSlope"
), dataList = list(structure(list(timeMillis = c(1591050080000, 
1591050252000, 1591050425000, 1591050598000, 1591050771000, 1591050944000, 
1591051116000, 1591051289000, 1591051462000, 1591051635000, 1591051808000, 
1591051980000, 1591052153000, 1591052326000, 1591052499000, 1591052672000, 
1591052844000, 1591053017000, 1591053190000, 1591053363000, 1591053536000
), localDateTime = c("2020-06-01T22:21:20", "2020-06-01T22:24:12", 
"2020-06-01T22:27:05", "2020-06-01T22:29:58", "2020-06-01T22:32:51", 
"2020-06-01T22:35:44", "2020-06-01T22:38:36", "2020-06-01T22:41:29", 
"2020-06-01T22:44:22", "2020-06-01T22:47:15", "2020-06-01T22:50:08", 
"2020-06-01T22:53:00", "2020-06-01T22:55:53", "2020-06-01T22:58:46", 
"2020-06-01T23:01:39", "2020-06-01T23:04:32", "2020-06-01T23:07:24", 
"2020-06-01T23:10:17", "2020-06-01T23:13:10", "2020-06-01T23:16:03", 
"2020-06-01T23:18:56"), value = c(-44.60666, 54.13578, 117.89097, 
82.92964, -66.7729, -114.94801, 54.97848, 91.05632, -8.16897, 
-71.04217, -88.72301, -139.31139, -50.58294, -8.78567, -8.39966, 
-54.48525, -110.99734, -25.96911, 100.79548, -81.94934, -196.21104
), quality = c("GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", 
"GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", 
"GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD")), class = "data.frame", row.names = c(NA, 
21L)), structure(list(timeMillis = c(1591050080000, 1591050252000, 
1591050425000, 1591050598000, 1591050771000, 1591050944000, 1591051116000, 
1591051289000, 1591051462000, 1591051635000, 1591051808000, 1591051980000, 
1591052153000, 1591052326000, 1591052499000, 1591052672000, 1591052844000, 
1591053017000, 1591053190000, 1591053363000, 1591053536000), 
    localDateTime = c("2020-06-01T22:21:20", "2020-06-01T22:24:12", 
    "2020-06-01T22:27:05", "2020-06-01T22:29:58", "2020-06-01T22:32:51", 
    "2020-06-01T22:35:44", "2020-06-01T22:38:36", "2020-06-01T22:41:29", 
    "2020-06-01T22:44:22", "2020-06-01T22:47:15", "2020-06-01T22:50:08", 
    "2020-06-01T22:53:00", "2020-06-01T22:55:53", "2020-06-01T22:58:46", 
    "2020-06-01T23:01:39", "2020-06-01T23:04:32", "2020-06-01T23:07:24", 
    "2020-06-01T23:10:17", "2020-06-01T23:13:10", "2020-06-01T23:16:03", 
    "2020-06-01T23:18:56"), value = c(-29.50907, 133.56741, 247.6122, 
    -391.21768, -118.63472, 67.12173, 235.18224, -275.67072, 
    223.91306, -28.00732, -368.20529, 250.14159, -361.28854, 
    408.80832, -115.09288, -453.42191, 260.57016, 261.98938, 
    -148.97125, -393.78149, 87.16415), quality = c("GOOD", "GOOD", 
    "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", 
    "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", 
    "GOOD", "GOOD", "GOOD")), class = "data.frame", row.names = c(NA, 
21L)))), row.names = 3:4, class = "data.frame")

Upvotes: 1

Views: 228

Answers (1)

Bas
Bas

Reputation: 4658

I don't know whether this works in your version of tidyr, but this gives what you want on my machine:

DF %>% 
  select(tagname, dataList) %>% 
  deframe() %>% 
  bind_rows(.id = "tagname") %>% 
  select(-timeMillis)

Upvotes: 2

Related Questions