'applying str_split_fixed function simultaneously across multiple columns in r
I have a data frame which is having multiple columns each column is having string values. I want to split column values by coma separator in the output data frame. Input and required output are as below
Col1=c("a,b,c","9,a,5")
Col2=c("c,b,e","4,r,t")
Col3=c("e,f,g","y,z,d")
Input=data.frame(Col1,Col2,Col3)
Column1=c("a","9")
Column2=c("b","a")
Column3=c("c","5")
Column4=c("c","4")
Column5=c("b","r")
Column6=c("e","t")
Column7=c("e","y")
Column8=c("f","z")
Column9=c("g","d")
Output=data.frame(Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9)
If anyone know the solution please help.
Solution 1:[1]
Sotos already provided a better suggestion, but here's an alternative dplyr/tidyr solution:
library(dplyr)
library(tidyr)
Input %>%
mutate(id = row_number()) %>%
pivot_longer(-id) %>%
separate(value, c('1', '2', '3')) %>%
pivot_wider(names_from=name, names_glue = "{name}_{.value}", values_from = '1':'3') %>%
select(-id)
#> # A tibble: 2 × 9
#> Col1_1 Col2_1 Col3_1 Col1_2 Col2_2 Col3_2 Col1_3 Col2_3 Col3_3
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 a c e b b f c e g
#> 2 9 4 y a r z 5 t d
Solution 2:[2]
With base R using read.csv
read.csv(text = do.call(paste, c(Input, sep = ",")), header = FALSE)
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 a b c c b e e f g
2 9 a 5 4 r t y z d
Solution 3:[3]
What could work for you would be some sort of a union:
$yearsInvoices = ModelOne::query()
->select(DB::raw('DISTINCT(YEAR(date)) as year'));
$yearsExpenses = ModelTwo::query()
->select(DB::raw('DISTINCT(YEAR(date)) as year'));
$allYears = DB::table($yearsInvoices->union($yearsExpenses))
->select(DB::raw('DISTINCT(year) as year'))
->pluck('year')->toArray();
This creates the union query (which will concatenate the two result tables), then uses the resulting union as the target for a new select which itself will select distinct years from this union resulting in (hopefully) all distinct years in each table
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aron |
| Solution 2 | akrun |
| Solution 3 | apokryfos |
