Below is an example, I want to use create a tibble in longer format with 3 columns
- Column 1; name - alpha; contains
aandb - Column 2; name - beta; contains
XandY - Column 3; name - values; contains values
I am getting an error
Error in UseMethod("pivot_longer") :
no applicable method for 'pivot_longer' applied to an object of class "character"
Is there way to create a longer tibble
- I believe all values that will go to values column, should be converted to numeric
- If a cell have multiple values, we may need to create an extra column
values2to hold it.
library(tidyverse)
# create empty list
b_X_values <- list()
b_Y_values <- list()
# fill list
for (iter in 1:2)
{
b_X_values[[iter]] <- runif(1)
b_Y_values[[iter]] <- runif(iter)
}
# tibble
tbl <- tibble(a_X_values = runif(2),
a_Y_values = runif(2),
b_X_values = b_X_values,
b_Y_values = b_Y_values)
# pivot tibble
tbl <- pivot_longer(cols = c("a_X_values", "a_Y_values",
"b_X_values", "b_Y_values"),
names_to = c("alpha", "beta", ".values"),
names_sep = "_")