Function to add word_id to a data.frame if it is not already there.
Value
A data.frame that includes a column called 'word_id' of type 'character' which contains a unique identifiers for all rows in the data.frame.
Details
Searches the data.frame for variables that include 'id' in their name. Checks if any of these is a unique identifier. If so, this variable is renamed 'word_id' and forced into type 'character'. If not, a new column named 'word_id' of type 'character' is created, containing a string of numbers that uniquely identify all rows in the data.frame.
Examples
t1 <- data.frame(wid = 1:10, a = sample(letters, 10))
t2 <- data.frame(id = rep(1, 10),
word_id = sprintf("%02d", 1:10),
a = sample(letters, 10))
t3 <- data.frame(a = sample(letters, 10))
add_word_id(t1)
#> a word_id
#> 1 b 1
#> 2 k 2
#> 3 w 3
#> 4 r 4
#> 5 x 5
#> 6 l 6
#> 7 v 7
#> 8 q 8
#> 9 m 9
#> 10 j 10
add_word_id(t2)
#> id word_id a
#> 1 1 01 a
#> 2 1 02 y
#> 3 1 03 f
#> 4 1 04 u
#> 5 1 05 d
#> 6 1 06 n
#> 7 1 07 p
#> 8 1 08 b
#> 9 1 09 j
#> 10 1 10 o
add_word_id(t3)
#> a word_id
#> 1 o 01
#> 2 l 02
#> 3 b 03
#> 4 a 04
#> 5 k 05
#> 6 c 06
#> 7 q 07
#> 8 x 08
#> 9 m 09
#> 10 d 10