r - split string at every character


I want to split every character in a string and output it as comma or tab separated characters: I need to use file_in and file_out since I have very many lines.





I have used this, but it prints every character vertically:

   use strict;
   use warnings;

    my $data = 'Becky Alcorn';

   my @values = split(undef,$data);

  foreach my $val (@values) {
   print "$val\n";

  exit 0;

7 Answers: 

In R, you can use strsplit and paste:

Strings <- c("TTTGGC","TTTG","TGCAATGG")
vapply(strsplit(Strings, ""), function(x) paste(x, collapse=","), character(1L))
# [1] "T,T,T,G,G,C"     "T,T,T,G"         "T,G,C,A,A,T,G,G"

You can write the output using writeLines, specifying sep = "\n" if required.


Your code uses a loop to print the values of @values one per line, so the computer does what you told it to. Try:

print join ",", @values;

or even condense your code all the way down to:

print join ",", split //, $data;

Using perl one-liner,

perl -F -lane 'print join ",", @F' file



awk one-liner:

awk -v FS='' -v OFS="," '$1=$1' file
open my $in, '<','in.txt';
open my $out, '>', 'out.txt';

    my @split = split(//);
    print $out join ",", @split;
    print $out "\n";

Or with the stringr package:

txt <- c("TTTGGC", "TTTG", "TGCAATGG") 
unlist(str_split(string=txt, pattern= ""))
 [1] ""  "T" "T" "T" "G" "G" "C" ""  "T" "T" "T" "G" ""  "T" "G" "C" "A" "A" "T" "G" "G"

Here's a quick and easy method that uses base R functions. The documentation for paste actually notes that paste can be used as the "the opposite" of strsplit

paste0 with collapse = "," is nice here

> txt <- c("TTTGGC", "TTTG", "TGCAATGG")  ## your data
> sapply(strsplit(txt, ""), paste0, collapse = ",")
# [1] "T,T,T,G,G,C"     "T,T,T,G"         "T,G,C,A,A,T,G,G"

More Articles

Split String With Numbers in R

This question already has an answer here: Split a string by any number of spaces 2 answers

Access remote git repo via ssh, using libgit2sharp

I'm trying to get a simple working example for cloning or accessing a remote git repository via ssh. After adding nuget package LibGit2Sharp-SSH v1.0.22, got a .Net Framework v4.6.1 console application like this:using LibGit2Sharp;using System;using System.IO;class Program{ static void Main(strin

javascript - onDelete not being called when deleting a node in Cloud Functions for Firebase

I am trying to build triggers for a firebase app around following and followers. Below is a snippet of my cloud code. I want to increase a counter when a user follows. For this I use oncreate (for when the user gains their first follower due the structure not existing until this point) and then I

php - mcrypt module open function is missing in php7.2.7

In PHP5 I was able to use mcrypt_module_open and now in PHP7.2.7 (which I just upgrade to) the function is missing and while searching, I discovered that it has been replaced by OpenSSL. My question is this: What is the equivalent of mcrypt_module_open in OPenSSL?

android - How to return different code and error with MockRestAdapter

I use MockRestAdapter to return mock data in my tests, but I'd also like to test errors (401, 503, UnknownHostException, etc)For SocketTimeoutException, there's an API, but how about different response code?I've tried MockWebServer but no matter what I enqueue, I always get a 200 with the mock data

java - gRPC: Generate certificateChainFile and privateKeyFile to make TLS/SSL work

I want to make tls/ssl connection between client and server(Not mutual, one way). This is my setup:Server: Server server = ServerBuilder.forPort(8443) // Enable TLS .useTransportSecurity(certChainFile, privateKeyFile) .addService(new GreetingServiceImpl()) .build();Cli

python - Heap that supports modification of its elements?

Here is my scenario. I want to implement A* (in Python) without having to resort to linear-time min or in operations. I need a heap to be able to efficiently get the lowest weight item. My immediate response was 'Easy! I'll use heapq!' Then I discovered that life is rarely as simple as we would like

java - Location mocking - google map detects the movement but my own application not triggering location change

I am facing a strange issue. I modified official mock provider source code provided by google to mock some route for my application.Using this code. mockLocation.setElapsedRealtimeNanos(elapsedTimeNanos); mockLocation.setTime(currentTime); // Set the loc

android - Can't find class [org/drinkless/td/libcore/telegram/TdApi$Object]

I am getting this error "Can't find class [org/drinkless/td/libcore/telegram/TdApi$Object]" but I haven't used this class anywhere in my project.I haven't used that class anywhere.But i am getting the above errorCan anyone please let me know how to solve the above error.

How do I cross-compile a Linux kernel to a MIPS little endian host?

The kernel in question is 2.6.18. If I callmake ARCH=mips CROSS_COMPILE=mipsel-linux- menuconfigthere will be only the option to build a big endian kernel in the menu. If I use ARCH=mipsel, it will complain about not having an arch/mipsel dir.How's this done?