Skip to content
Prev 366437 / 398502 Next

How might I work with a data.frame where each physical row represents several logical rows?

Perhaps something like this:

# function to read the values in 'values':
 parse_values <- function(x) {scan(text= gsub( "\\[|\\]","",x), sep=",") }

# the apply function reads line-by-line
 new_dat <- apply(test_data, 1, function(d) data.frame( as.list(d[!names(d)  %in% "values"]), nvals <- parse_values(d['values']) ) )
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items
Read 4 items

# Could suppress the report from scan by adding quiet = TRUE
# now take this list of 4 line data.frames and "rbind" them
# If you wanted these to remain character you would use stringsAsFactors=FALSE in the data.frame call
start         hostname  mtype limit_type   hw         fw tcp_state value_type nic
1 1482793200 c001.example.net health       fill 1.16 2017Q1.1.1                limit all
2 1482793200 c001.example.net health       fill 1.16 2017Q1.1.1                limit all
3 1482793200 c001.example.net health       fill 1.16 2017Q1.1.1                limit all
4 1482793200 c001.example.net health       fill 1.16 2017Q1.1.1                limit all
5 1482793200 c001.example.net health      serve 1.16 2017Q1.1.1                limit all
6 1482793200 c001.example.net health      serve 1.16 2017Q1.1.1                limit all
                name nvals....parse_values.d..values...
1 in_download_window                                  0
2 in_download_window                                  0
3 in_download_window                                  0
4 in_download_window                                  0
5 in_download_window                                  0
6 in_download_window                                  0

str(new_df)
'data.frame':	60 obs. of  11 variables:
 $ start                             : Factor w/ 1 level "1482793200": 1 1 1 1 1 1 1 1 1 1 ...
 $ hostname                          : Factor w/ 2 levels "c001.example.net",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ mtype                             : Factor w/ 3 levels "health","net",..: 1 1 1 1 1 1 1 1 2 2 ...
 $ limit_type                        : Factor w/ 3 levels "fill","serve",..: 1 1 1 1 2 2 2 2 3 3 ...
 $ hw                                : Factor w/ 2 levels "1.16","1.21": 1 1 1 1 1 1 1 1 1 1 ...
 $ fw                                : Factor w/ 2 levels "2017Q1.1.1","2016Q4.2.13": 1 1 1 1 1 1 1 1 1 1 ...
 $ tcp_state                         : Factor w/ 6 levels "","closed","closing",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value_type                        : Factor w/ 2 levels "limit","": 1 1 1 1 1 1 1 1 2 2 ...
 $ nic                               : Factor w/ 3 levels "all","","mce0": 1 1 1 1 1 1 1 1 2 2 ...
 $ name                              : Factor w/ 8 levels "in_download_window",..: 1 1 1 1 1 1 1 1 2 2 ...
 $ nvals....parse_values.d..values...: num  0 0 0 0 0 ...
I thought you wanted the data in long form.
David Winsemius
Alameda, CA, USA