Print Tibble with Column Breaks as in V1.3.0

Print tibble with column breaks as in v1.3.0

Try out this function:

print_width_inf <- function(df, n = 6) {
df %>%
head(n = n) %>%
as.data.frame() %>%
tibble:::shrink_mat(width = Inf, rows = NA, n = n, star = FALSE) %>%
`[[`("table") %>%
print()
}

Better way to add tibble column to df in iterative fashion

tibbles square bracket subsetting doesn't drop by default, so it always stays a tibble.

Just change

df$output = df_tibble[, i]

to

df$output = df_tibble[, i, drop = TRUE]

How to change column data type of a tibble (with least typing)

This solution is probably the shortest:

mtcars$qsec %<>% as.integer

The trick is to perform the cast operation directly on the column > no need for mutate() any more.

add a tibble to a second one with an additional modification

We may first rbind the matching columns and then add the other one with recycling:

cbind(rbind(tb1[-3], tb2), tb1["tarima"])
# symbol open tarima
# 1 A 25.3 59.5
# 2 A 27.1 61.7
# 11 B 60.8 59.5
# 21 B 60.3 61.7
# 3 CD 32.4 59.5
# 4 CD 33.6 61.7

Create a tibble with tribble using defaults that don't cause an error, e.g. like tibble

If your use case is easily reading a table with unknown structure into a data frame, you can do it like this.

data <- list("Header1_A", "Header2_A",
0.19, 0.20,
"Header1_B", "Header2_B",
0.19, 0.20)

df <- as.data.frame(matrix(unlist(data), ncol = 2, byrow = TRUE), stringsAsFactors = FALSE)

df
#> V1 V2
#> 1 Header1_A Header2_A
#> 2 0.19 0.2
#> 3 Header1_B Header2_B
#> 4 0.19 0.2

Created on 2020-05-24 by the reprex package (v0.3.0)

To preserve column types (if possible) you could send the data list to this

dribble <- function(data, ncol){
df <- list()
for (i in 1:ncol){
x <- unlist(data[seq(i, length(data), ncol)])
df[[paste0("X", i)]] <- x
}
as.data.frame(df, stringsAsFactors = FALSE)
}

I need to copy numbers between two text strings in column, transpose it to columns next to the first text string

Split on a regex-based logical:

(I defined the break points based on the presence of capital letters (i.e. [A-Z]); you may want to modify the pattern based on your expected break points in df1$column1.)

a <- split(df1$column1, cumsum(grepl('[A-Z]', df1$column1)))
a
$`1`
[1] "STOUT" "18" "9341" "4" "0,2005"

$`2`
[1] "STOUT" "1" "9341" "25" "0,2004"

$`3`
[1] "STIN" "7" "9341" "0,2003"

$`4`
[1] "OFF" "7"

$`5`
[1] "L(1)" "9342" "0,2005"

Then rbind() and fill with NA:

(plyr::rbind.fill() expects a dataframe, so I'm using lapply() to call as.data.frame() to each list element.)

library(plyr)

plyr::rbind.fill(lapply(a,function(y){as.data.frame(t(y),stringsAsFactors=FALSE)}))
V1 V2 V3 V4 V5
1 STOUT 18 9341 4 0,2005
2 STOUT 1 9341 25 0,2004
3 STIN 7 9341 0,2003 <NA>
4 OFF 7 <NA> <NA> <NA>
5 L(1) 9342 0,2005 <NA> <NA>

Fit model using each predictor columns indiviually store results in dataframe

1) This gives the glance and tidy output for the model fits:

library(broom)

make_model <- function(nm) lm(iris[c("Sepal.Length", nm)])
fits <- Map(make_model, iris_vars)

glance_tidy <- function(x) c(unlist(glance(x)), unlist(tidy(x)[, -1]))
out <- sapply(fits, glance_tidy)

1a) or as a magrittr pipeline:

library(magrittr)
out <- iris_vars %>% Map(f = make_model) %>% sapply(glance_tidy)

Either one gives the following matrix:

> out
Sepal.Width Petal.Length Petal.Width
r.squared 1.382265e-02 7.599546e-01 6.690277e-01
adj.r.squared 7.159294e-03 7.583327e-01 6.667914e-01
sigma 8.250966e-01 4.070745e-01 4.779948e-01
statistic 2.074427e+00 4.685502e+02 2.991673e+02
p.value 1.518983e-01 1.038667e-47 2.325498e-37
df 2.000000e+00 2.000000e+00 2.000000e+00
logLik -1.829958e+02 -7.702021e+01 -1.011107e+02
AIC 3.719917e+02 1.600404e+02 2.082215e+02
BIC 3.810236e+02 1.690723e+02 2.172534e+02
deviance 1.007561e+02 2.452503e+01 3.381489e+01
df.residual 1.480000e+02 1.480000e+02 1.480000e+02
estimate1 6.526223e+00 4.306603e+00 4.777629e+00
estimate2 -2.233611e-01 4.089223e-01 8.885803e-01
std.error1 4.788963e-01 7.838896e-02 7.293476e-02
std.error2 1.550809e-01 1.889134e-02 5.137355e-02
statistic1 1.362763e+01 5.493890e+01 6.550552e+01
statistic2 -1.440287e+00 2.164602e+01 1.729645e+01
p.value1 6.469702e-28 2.426713e-100 3.340431e-111
p.value2 1.518983e-01 1.038667e-47 2.325498e-37

or transposed:

> t(out)
r.squared adj.r.squared sigma statistic p.value df
Sepal.Width 0.01382265 0.007159294 0.8250966 2.074427 1.518983e-01 2
Petal.Length 0.75995465 0.758332718 0.4070745 468.550154 1.038667e-47 2
Petal.Width 0.66902769 0.666791387 0.4779948 299.167312 2.325498e-37 2
logLik AIC BIC deviance df.residual estimate1
Sepal.Width -182.99584 371.9917 381.0236 100.75610 148 6.526223
Petal.Length -77.02021 160.0404 169.0723 24.52503 148 4.306603
Petal.Width -101.11073 208.2215 217.2534 33.81489 148 4.777629
estimate2 std.error1 std.error2 statistic1 statistic2
Sepal.Width -0.2233611 0.47889634 0.15508093 13.62763 -1.440287
Petal.Length 0.4089223 0.07838896 0.01889134 54.93890 21.646019
Petal.Width 0.8885803 0.07293476 0.05137355 65.50552 17.296454
p.value1 p.value2
Sepal.Width 6.469702e-28 1.518983e-01
Petal.Length 2.426713e-100 1.038667e-47
Petal.Width 3.340431e-111 2.325498e-37

2) If we remove the first unlist from the glance_tidy function definition then we get a 2d list (rather than a 2d numeric matrix):

glance_tidy_l <- function(x) c(glance(x), unlist(tidy(x)[, -1]))
iris_vars %>% Map(f = make_model) %>% sapply(glance_tidy_l)

Sepal.Width Petal.Length Petal.Width
r.squared 0.01382265 0.7599546 0.6690277
adj.r.squared 0.007159294 0.7583327 0.6667914
sigma 0.8250966 0.4070745 0.4779948
statistic 2.074427 468.5502 299.1673
p.value 0.1518983 1.038667e-47 2.325498e-37
df 2 2 2
logLik -182.9958 -77.02021 -101.1107
AIC 371.9917 160.0404 208.2215
BIC 381.0236 169.0723 217.2534
deviance 100.7561 24.52503 33.81489
df.residual 148 148 148
estimate1 6.526223 4.306603 4.777629
estimate2 -0.2233611 0.4089223 0.8885803
std.error1 0.4788963 0.07838896 0.07293476
std.error2 0.1550809 0.01889134 0.05137355
statistic1 13.62763 54.9389 65.50552
statistic2 -1.440287 21.64602 17.29645
p.value1 6.469702e-28 2.426713e-100 3.340431e-111
p.value2 0.1518983 1.038667e-47 2.325498e-37


Related Topics



Leave a reply



Submit