/
fs_tib_string.Rmd
75 lines (61 loc) · 1.87 KB
/
fs_tib_string.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: "R Tibble Dataframe String Manipulations"
description: |
There are multiple CEV files, each containing the same file structure but simulated
with different parameters, gather a subset of columns from different files, and provide
with correct attributes based on CSV file names.
core:
- package: r
code: |
cbind(ls_st, ls_st)
as_tibble(mt_st)
date: 2021-01-10
date_start: 2020-04-01
output:
pdf_document:
pandoc_args: '../../_output_kniti_pdf.yaml'
includes:
in_header: '../../preamble.tex'
html_document:
pandoc_args: '../../_output_kniti_html.yaml'
includes:
in_header: "../../hdga.html"
always_allow_html: true
urlcolor: blue
---
### String Dataframes
```{r global_options, include = FALSE}
try(source("../../.Rprofile"))
```
`r text_shared_preamble_one`
`r text_shared_preamble_two`
`r text_shared_preamble_thr`
#### List of Strings to Tibble Datfare
There are several lists of strings, store them as variables in a dataframe.
```{r}
# Sting data inputs
ls_st_abc <- c('a', 'b', 'c')
ls_st_efg <- c('e', 'f', 'g')
ls_st_opq <- c('o', 'p', 'q')
mt_str = cbind(ls_st_abc, ls_st_efg, ls_st_opq)
# Column Names
ar_st_varnames <- c('id','var1','var2','var3')
# Combine to tibble, add name col1, col2, etc.
tb_st_combine <- as_tibble(mt_str) %>%
rowid_to_column(var = "id") %>%
rename_all(~c(ar_st_varnames))
# Display
kable(tb_st_combine) %>% kable_styling_fc()
```
#### Find and Replace
Find and Replace in Dataframe.
```{r, amto.tibble.fs_tib_string.find_replace, eval=FALSE}
# if string value is contained in variable
("bridex.B" %in% (df.reg.out.all$vars_var.y))
# if string value is not contained in variable:
# 1. type is variable name
# 2. Toyota|Mazda are strings to be excluded
filter(mtcars, !grepl('Toyota|Mazda', type))
# filter does not contain string
rs_hgt_prot_log_tidy %>% filter(!str_detect(term, 'prot'))
```