r - Counting true/false in complex data frame -


i trying rather complex in r, , not sure start.

i have dataframe looks sort of this:

main_val sub_val bit_one bit_two  1             1       1  1             1       0  1             1       1  1      b        1       0  2             1       1  2      b        1       1  2             1       1 

now count number of 0s, 1s, 2s, , 3s represented bits each sub value of each main value. should return:

main_val sub_val  0s  1s  2s  3s  1            0   0   1   2  1       b      0   0   1   0  2            0   0   0   2  2       b      0   0   0   1 

any thought on how this? can think of ugly loops take forever (this run on alot of data).

pardon earlier comment - think need table() , reshape() in base r. may slow if have huge amount of data, however, @ point suggest investigating data.table.

# start turning of stringsasfactors options(stringsasfactors = false)  # create fake data fake.data <- data.frame(main_val = c("one","one","one","one","two","two","two"),                         sub_val = c("a","a","a","b","a","b","a"),                         bit_one = c(1,1,1,1,1,1,1),                         bit_two = c(1,0,1,0,1,1,1))  # generate decimal representation of 2 bits fake.data$decimal <- fake.data$bit_one*1 +fake.data$bit_two*2  # create table of results, reshape fake.data.summary <- as.data.frame(table(main=fake.data$main_val,                                          sub=fake.data$sub_val,                                          value=fake.data$decimal))  fake.data.summary <- reshape(data = fake.data.summary,                              v.names = "freq",                              idvar = c("main","sub"),                              timevar = "value",                              direction = "wide") 

note in example, 1 , 3 in output, since 1 , 3 in input. if uniform outputs desired despite may or may not present, may need sanitization of output - suspect don't need that, have enough volume ensure 0 through 3 represented.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -