CRich
CRich

Reputation: 118

Merge a column into a flat file and separate chars

So I am trying to convert a column of numbers into a one string, then write each character (number in this case) into a csv as follows. If there is a better way to solve my problem, please do tell.

set.seed(123)
x = data.frame(numbers = sample(001:999, replace = F))

numbers
847
95
350
298
682
806
98

to a text file that just has 8479535029862898

to a csv file (END GOAL) 8,4,7,9,6,0,2,9,8,6,2,8,9,8

Upvotes: 0

Views: 40

Answers (1)

akrun
akrun

Reputation: 887611

In R, we can paste the elements together and strsplit the whole string by specifying the split as ""

out <- as.numeric(strsplit(paste(x$numbers, collapse = ""), "")[[1]])

and write it to a file

cat(out, file = "fileout.txt")

If we are using python

import pandas as pd
import numpy as np
x = pd.DataFrame({'numbers': np.random.choice(range(1, 1000), size = 999, replace = True)})
list(map(int, list("".join(map(str, list(x['numbers']))))))

Upvotes: 2

Related Questions