This question already has answers here :
you can use the strsplit
:
table(dt[,unlist(strsplit(sample,",")),by=id])
V1
id MER-1 MER-2 MER-3 MER-4 MER-5 MER_3
id1 1 0 1 1 0 0
id2 0 0 0 0 1 0
id3 0 1 0 0 0 0
id4 0 1 1 1 1 0
id5 0 0 0 0 0 1
id6 0 0 0 0 1 0
Convert String into Binary Sequence, In each iteration store ASCII value of character in variable val and then convert it into binary number and store result in array finally print the� List of strings to binary matrix [duplicate] Ask Question Asked 23 days ago. Active 23 days ago. Viewed 62 times 4. 0. This question already has answers
Using dt
from the Note at the end which fixed the typo in the question, use separate_rows
to expand the data row by row and then use table
to compute the counts.
library(data.table)
library(dplyr)
library(tidyr)
dt %>%
separate_rows(sample, sep = ",") %>%
table
giving:
sample
id MER-1 MER-2 MER-3 MER-4 MER-5
id1 1 0 1 1 0
id2 0 0 0 0 1
id3 0 1 0 0 0
id4 0 1 1 1 1
id5 0 0 1 0 0
id6 0 0 0 0 1
Note
library(data.table)
dt <- data.table(id = c('id1','id2','id3','id4','id5','id6'),
sample = c("MER-1,MER-3,MER-4","MER-5","MER-2","MER-2,MER-3,MER-4,MER-5","MER-3","MER-5" ))
Data Type Conversion - MATLAB & Simulink, str2num, Convert character array or string to numeric array bin2dec, Convert text representation of binary number to decimal number. dec2base, Convert� Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Follow the steps below to convert an adjacency list to an adjacency matrix: Initialize a matrix with 0s.
You can also use the library splitstackshape
:
table(cSplit(dt, "sample", sep = ",", direction = "long"))
sample
id MER-1 MER-2 MER-3 MER-4 MER-5
id1 1 0 1 1 0
id2 0 0 0 0 1
id3 0 1 0 0 0
id4 0 1 1 1 1
id5 0 0 1 0 0
id6 0 0 0 0 1
Or using cSplit_e
created exactly for this scenario (provided by @A5C1D2H2I1M1N2O1R2T1):
cSplit_e(dt, "sample", sep = ",", type = "character", fill = 0, drop = TRUE)
How can I store binary strings into an array?, I have a character string that I converted into a ASCII format. But I don't know how will I put the individual numbers into an array. For example I� Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list() function. Creating a List. Following is an example to create a list containing strings, numbers, vectors and a logical values.
Convert byte[] Array to String in Java, Conversion between byte array and string may be used in IO there to serve specific purposes i.e. strings are for text, byte[] is for binary data. We can do using join() function, but we need to first convert list of different type of elements to a list of strings. For that we need to call str() on each item of the list to convert it to string. Then we can join all the items in the new list of strings to create a string. For example,
How to convert Byte[] array to String in Java, The value returned by array.toString() is *not* the binary representation of the array's elements. It is the default toString() method implemented� XOR two binary strings of unequal lengths; Generate string with Hamming Distance as half of the hamming distance between strings A and B; Count number of binary strings without consecutive 1’s : Set 2; Count binary strings with twice zeros in first half; Number of binary strings such that there is no substring of length ≥ 3; Number of sub
Convert between byte array/slice and string � YourBasic Go, Builder together with the fmt package for a clean and simple way to build strings efficiently without redundant copying. yourbasic.org. Conversions [complete list]. Python | Converting all strings in list to integers Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the entire list of string to integers is quite common in development domain.
Comments You should use cSplit_e
for this purpose. @A5C1D2H2I1M1N2O1R2T1 looks like a nice option, however, I cannot get it work on this data. cSplit_e(dt, "sample", sep = ",", fill = 0)
throws me an error. Add a type = "character"
in there... @A5C1D2H2I1M1N2O1R2T1 thank you, it is really a neat function :)