Split delimited single value character vector

sag

I have a user provided String like "Mon,Tue,Wed,Thu,Fri". Please note that this value is user provided input. User may provide something like "Mon,Tue,Wed" and so on.

I want to get this as vector which will be used for plotting and for further analytics.

Since the value provided by user is a single comma delimited value, we need to separate the value in to individual values and then construct vector.

Is there any way to construct vector directly.

i.e I should get a vector from "Mon,Tue,Wed,Thu,Fri". As expected, below code returns a single value vector.

> weekdays <- c(days)

> print(weekdays)
[1] "Mon,Tue,Wed,Thu,Fri"

But I need something like below

> days <- c("Mon","Tue","Wed","Thu","Fri")
> print(days)
[1] "Mon" "Tue" "Wed" "Thu" "Fri"

Note that I am not reading a CSV file. I am just trying to read a user provided single CSV row as vector.

Jaap

You can use strsplit for that:

wkdays <- "Mon,Tue,Wed,Thu,Fri"
unlist(strsplit(wkdays, ","))

this gives:

> unlist(strsplit(wkdays, ","))
[1] "Mon" "Tue" "Wed" "Thu" "Fri"

An alternative is to use scan:

scan(text = wkdays, sep = ",", what = character())

which gives the same result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Split character vector into sentences

Split character vector values by fixed delimiter - return data frame with one row for each vector value

Split character vector by element in R?

Split character vector into named list of character vectors

Group by a single character otherwise split

How to split a multi-valued, character delimited column into multiple rows?

Create UDF to split row into comma delimited single Column

How to split a character vector into data frame?

Split character vector at math comparisons signs in R

How to split a character vector based on length of a list

Is there a way to split a character vector into two columns?

How to split on a character and keep value

Split dataframe character into columns based on value of character

Character value to numeric vector in dataframe

Entire character vector saved as a single string in R

how to convert single character vector to numeric in R?

VBA - How to remove value from string delimited with character

Replace | character between the records with a value in a pipe delimited file

PHP How to split delimited string to key value pairs

how to properly split a tab delimited value into individual columns in ORACLE

Split a column of values delimited by a space into separate columns for each value in python

Excel: Split a delimited string and retrieve a numerical value of each element in the string

How do I split this delimited text in to key and value using a Regex?

How to split a character vector based on a numeric vector for positions

Pandas choose a single value from delimited list in column based on date

SQL query to get / delimited column value in single row

Split a delimited string into an object

Split with specials character and print string value

Split a pipe-delimited series, groupby a separate series, and return the counts of each split value in new columns

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive