Is There a General Inverse of The Table() Function

Creating a table with individual trials from a frequency table in R (inverse of table function)

You may try this:

# create 'result' vector
# repeat 1s and 0s the number of times given in the respective 'count' column
result <- rep(rep(c(1, 0), nrow(df)), unlist(df[ , c("success.count", "fail.count")]))

# repeat each row in df the number of times given by the sum of 'count' columns
data.frame(df[rep(1:nrow(df), rowSums(df[ , c("success.count", "fail.count")]) ), c("factor.A", "factor.B")], result)

# factor.A factor.B result
# 1 0 1 0
# 1.1 0 1 0
# 2 1 1 1
# 2.1 1 1 1
# 2.2 1 1 0

Is there a function to invert the number of occurrences of values in a data.table?

If we use the approach by the OP, then just replicate the rows by the reverse of 'N' and assign 'N' to NULL

initially[, .N, by = initially][rep(seq_len(.N), rev(N))][, N := NULL][]

General table into a quartely table

Change Date to date class, extract year from it and cast data into wide format.

library(dplyr)

df %>%
mutate(Time = as.Date(Time),
year = format(Time, '%Y')) %>%
select(-Time, -Quarters) %>%
tidyr::pivot_wider(names_from = Season, values_from = Qty, values_fn = sum)
#OR in old tidyr
#tidyr::pivot_wider(names_from = Season, values_from = Qty,
# values_fn = list(Qty = sum))

# A tibble: 3 x 5
# year Winter Spring Summer Fall
# <chr> <dbl> <dbl> <dbl> <dbl>
#1 2017 4335. 1645. 4442. 4452.
#2 2018 6670. 8773. 10498. 9843.
#3 2019 0 559. 528. NA

An alternative, instead of using values_fn we can sum it first and then cast to wide.

df %>%
type.convert(as.is = TRUE) %>%
mutate(Time = as.Date(Time),
year = format(Time, '%Y')) %>%
select(-Time, -Quarters) %>%
group_by(year, Season) %>%
summarise(Qty = sum(Qty)) %>%
tidyr::spread(Season, Qty)
#OR
#tidyr::pivot_wider(names_from = Season, values_from = Qty)

SQL: Reverse transpose a table

You could do this very simply with a UNION clause:

Select Scan_ID, 'A' as Region, A_Volume as volume
union all
Select Scan_ID, 'B' as Region, B_Volume as volume
union all
Select Scan_ID, 'C' as Region, C_Volume as volume

table() function in r - is there a better way with e.g., dplyr?

Another approach is to use tables::tabular() as follows.

textData <- "id Country                        Relationship_type
1 Algeria 2
2 Bulgaria 1
3 USA 2
4 Algeria 3
5 Germany 2
6 USA 1
7 Algeria 1
8 Bulgaria 3
9 USA 2
10 Algeria 2
11 Germany 1
12 USA 3"

df <- read.table(text=textData,header=TRUE)
library(tables)
tabular(Factor(Country) ~ Factor(Relationship_type),data=df)

...and the output:

          Relationship_type    
Country 1 2 3
Algeria 1 2 1
Bulgaria 1 0 1
Germany 1 1 0
USA 1 2 1

Still another approach is to recast the output from table() as a data frame, and pivot it wider with tidyr::pivot_wider().

# another approach: recast table output as data.frame
tableData <- data.frame(table(df$Country,df$Relationship_type))
library(dplyr)
library(tidyr)
tableData %>%
pivot_wider(id_cols = Var1,
names_from = Var2,
values_from = Freq)

...and the output:

> tableData %>% 
+ pivot_wider(id_cols = Var1,
+ names_from = Var2,
+ values_from = Freq)
# A tibble: 4 x 4
Var1 `1` `2` `3`
<fct> <int> <int> <int>
1 Algeria 1 2 1
2 Bulgaria 1 0 1
3 Germany 1 1 0
4 USA 1 2 1

If we add a dplyr::rename() to the pipeline, we can rename the Var1 column to Country.

tableData %>% 
pivot_wider(id_cols = Var1,
names_from = Var2,
values_from = Freq) %>%
rename(Country = Var1)

As usual, there are many ways in R to accomplish this task. Depending on the reason why the desired output is a CSV file, there are a variety of approaches that could fit the requirements. If the ultimate goal is to create presentation quality tables, then it's worth a look at this summary of packages that create presentation quality tables: How gt fits with other packages that create display tables.

Find the inverse function in R

The package investr is able to apply an inverse regression.

How to check if a matrix has an inverse in the R language

@MAB has a good point. This uses solve(...) to decide if the matrix is invertible.

f <- function(m) class(try(solve(m),silent=T))=="matrix"
x <- matrix(rep(1,25),nc=5) # singular
y <- matrix(1+1e-10*rnorm(25),nc=5) # very nearly singular matrix
z <- 0.001*diag(1,5) # non-singular, but very smalll determinant
f(x)
# [1] FALSE
f(y)
# [1] TRUE
f(z)
# [1] TRUE


Related Topics



Leave a reply



Submit