Function to add 'id' to a data.frame if it is not already there.
Value
A data.frame that includes a column called '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(id = 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_id(t1)
#> id a
#> 1 1 t
#> 2 2 g
#> 3 3 j
#> 4 4 o
#> 5 5 b
#> 6 6 h
#> 7 7 w
#> 8 8 k
#> 9 9 m
#> 10 10 y
add_id(t2)
#> id a
#> 1 01 v
#> 2 02 y
#> 3 03 m
#> 4 04 e
#> 5 05 a
#> 6 06 x
#> 7 07 u
#> 8 08 p
#> 9 09 w
#> 10 10 k
add_id(t3)
#> a id
#> 1 q 01
#> 2 k 02
#> 3 h 03
#> 4 d 04
#> 5 p 05
#> 6 e 06
#> 7 l 07
#> 8 g 08
#> 9 m 09
#> 10 v 10