How to convert list of characters to columns

convert one column from factor to character in r
r convert numeric to character
python split string into list of characters
convert all numeric variables to character r
r convert dataframe column to character vector
convert data frame column to character
tibble list column
r convert table to character

I have a data frame, column C is character

df <- data.frame(A = c(13, 15, 17), B = c("yes", "no", "yes"), C = c("Mon, Thu, Sun", "Thu, Tue, Fri", "Sat, Mon, Wen"))

    A   B   C
1   13  yes Mon, Thu, Sun
2   15  no  Thu, Tue, Fri
3   17  yes Sat, Mon, Wen

How can I convert data.frame column C to:

    A   B   Sun Mon Tue Wen Thu Fri Sat
1   13  yes 1   1   0   0   1   0   0
2   15  no  0   0   1   0   1   1   0
3   17  yes 0   1   0   1   0   0   1

One dplyr and tidyr option could be:

df %>%
 mutate(C = strsplit(as.character(C), ", ", fixed = TRUE)) %>%
 unnest(C) %>%
 mutate(C_val = 1) %>%
 pivot_wider(names_from = C, values_from = C_val, values_fill = list(C_val = 0))

      A B       Mon   Thu   Sun   Tue   Fri   Sat   Wen
  <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1    13 yes       1     1     1     0     0     0     0
2    15 no        0     1     0     1     1     0     0
3    17 yes       1     0     0     0     0     1     1

Or altenatively:

df %>%
 separate_rows(C) %>%
 mutate(C_val = 1) %>%
 pivot_wider(names_from = C, values_from = C_val, values_fill = list(C_val = 0))

List columns, Lists as variables in a data frame. One row per GoT character. List columns for aliases and allegiances. x <- tibble( name = got_chars %>% map_chr(  In addition to being a tool for converting different date formats, Text to Columns can also convert international number formats. Here in the UK, a decimal point is used in number formats. So for example, the number 1,064.34 is a little more than one thousand.

Convert it to long form using separate_rows, add a value column and convert C to factor and then spread it back to wide form.

library(dplyr)
library(tidyr)

days.abb <- c("Sun", "Mon", "Tue", "Wen", "Thu", "Fri", "Sat")
df %>%
  separate_rows(C) %>%
  mutate(value = 1, C = factor(C, days.abb)) %>%
  spread(C, value, fill = 0)

giving:

   A   B Sun Mon Tue Wen Thu Fri Sat
1 13 yes   1   1   0   0   1   0   0
2 15  no   0   0   1   0   1   1   0
3 17 yes   0   1   0   1   0   0   1

Python, This approach uses for loop to convert each character into a list. Split strings into two List/Columns using str.split() · Python | Splitting string to list of characters​  Text to Columns is the perfect way to get this sorted. Suppose you have a dataset as shown below: Here are the steps to convert these trailing minuses into negative numbers: Select the data set. Go to Data → Data Tools → Text to Columns. This will open the Convert Text to Columns Wizard. In Step 1, click on ‘Next’. In Step 2, click on

An approach with base R:

  1. Applied to the C column, substitute ",\\s" (comma followed by a white-space) by a "|"
  2. Use grepl with the resulting regular expressions to check which days in days.abb are present
  3. Combine the resulting binary vectors by rows and cbind to the existing A and B column
## data
df <- data.frame(
    A = c(13, 15, 17), 
    B = c("yes", "no", "yes"), 
    C = c("Mon, Thu, Sun", "Thu, Tue, Fri", "Sat, Mon, Wen")
)

## abbreviated weekdays
days.abb <- c("Sun", "Mon", "Tue", "Wen", "Thu", "Fri", "Sat")

## find weekday indices for each character in column C
df1 <- cbind(df[, -3], t(sapply(df[, 3], function(x) 1 * grepl(gsub(",\\s", "|", x), days.abb))))

## update column names
setNames(df1, c("A", "B", days.abb)) 
#>    A   B Sun Mon Tue Wen Thu Fri Sat
#> 1 13 yes   1   1   0   0   1   0   0
#> 2 15  no   0   0   1   0   1   1   0
#> 3 17 yes   0   1   0   1   0   0   1

toString: Convert an R Object to a Character String, toString: Convert an R Object to a Character String The default method first converts x to character and then concatenates the elements separated by ", " . nrow: The Number of Rows/Columns of an Array ns-dblcolon: Double Colon and​  When you’re splitting text to columns, Excel needs to know where to make the splits. With fixed-width splitting, Excel splits the text at specific character counts. So it might split the text at the 5th, 15th, and 27th characters. Delimited splitting, on the other hand, takes place when Excel sees certain characters. It might split the text

How to convert dataframe columns from factors to characters , I guess these are indices into the levels of the factors of the columns. How to convert a list of dataframes in to a single dataframe using R? Select the cells you'd like to convert to numbers, and make sure their format is set to General. Switch to the Data tab, Data Tools group, and click the Text to Columns button. In step 1 of the Convert Text to Columns Wizard, select Delimited under Original data type, and click Finish. That's all there is to it!

Convert List to Data Frame in R (2 Examples), One of them is numeric and one of them is a character. Example 1: Convert List to Data Frame Columns. If we want to convert each of the two list  Convert All Characters of a Data Frame to Numeric. As you have seen, to convert a vector or variable with the character class to numeric is no problem. However, sometimes it makes sense to change all character columns of a data frame or matrix to numeric. Consider the following R data.frame:

Convert Character to Numeric in R (Example for String Variable , In the video, I'm explaining how to convert character and factors to numeric in R: However, in many situations it is better to convert only character columns to numeric (i.e. not column X3, Convert Data Frame Rows to List in R (Example)  Convert Data Frame Column to Numeric in R (2 Examples) | Change Factor, Character & Integer In this R tutorial, I’ll explain how to convert a data frame column to numeric in R . No matter if you need to change the class of factors, characters, or integers , this tutorial will show you how to do it.

Comments
  • thx, I will try your solution, too. Maybe I would need right sequence of days.