There may be a little you need to tweak based on data types—I pasted in what you have here, which is that columns without checkmarks are just blank.
The method that I used is to create row numbers to identify the observations that you start out with, convert to long-shaped data, group by row number, find states that are checked off, collapse them into one string, and reshape back to a wide format. The reason for doing it this way is that it will scale well—it doesn't matter how many states there are, because I'm not doing something like Texas == "X" & California == "X" & ... that would require hardcoding.
The first major step is using tidyr::gather so you have rows, all possible values of states, and the checkmarks or blanks.
library(tidyverse) df <- "Column1 Column2 Column3 Column4 X X X X X X X X" %>% read_table() df %>% setNames(c("Texas", "California", "New Jersey", "Oklahoma")) %>% mutate(row = row_number()) %>% gather(key = state, value = value, -row) #> # A tibble: 12 x 3 #> row state value #> <int> <chr> <chr> #> 1 1 Texas X #> 2 2 Texas X #> 3 3 Texas X #> 4 1 California X #> 5 2 California "" #> 6 3 California "" #> 7 1 New Jersey X #> 8 2 New Jersey X #> 9 3 New Jersey "" #> 10 1 Oklahoma "" #> 11 2 Oklahoma X #> 12 3 Oklahoma X
Then I group by the row numbers, and use a stringr convenience function. str_which(value, "^X$") finds the locations where value contains the regex ^X$. Using this as the indices of state gets the entries in state that correspond to an X in value. Then I collapse those strings into a single string column, and use tidyr::spread to make it back into a wide format.
df %>% setNames(c("Texas", "California", "New Jersey", "Oklahoma")) %>% mutate(row = row_number()) %>% gather(key = state, value = value, -row) %>% group_by(row) %>% mutate(states = state[str_which(value, "^X$")] %>% paste(collapse = ", ")) %>% spread(key = state, value = value) #> # A tibble: 3 x 6 #> # Groups: row [3] #> row states California `New Jersey` Oklahoma Texas #> <int> <chr> <chr> <chr> <chr> <chr> #> 1 1 Texas, California, New Jer… X X "" X #> 2 2 Texas, New Jersey, Oklahoma "" X X X #> 3 3 Texas, Oklahoma "" "" X X
Created on 2018-10-11 by the reprex package (v0.2.1)
Texas, New Jersey