How to read one aligned CSV in R?

I recently worked on a dummy dataset, and I found out that the data provided to me was on the same line. A similar example for him is depicted as follows:

Name,Age,Gender,Occupation A,10,M,Student B,11,M,Student C,11,F,Student

I want to import data and get output as follows:

Name  Age  Gender  Occupation
 A    10     M       Student
 B    11     M       Student
 C    12     F       Student

a case may arise where the value may be absent. importing such data requires logic. Can anyone help me build the import logic of such datasets.

I tried regular import, but it really didn't help. just imported the file using the function read.csv(), and this did not give me the expected result.

EDIT: what if the data is similar:

Name,Age,Gender,Occupation ABC XYZ,10,M,Student B,11,M,Student C,11,F,Student

and I need a conclusion like:

  Name     Age  Gender  Occupation
 ABC XYZ    10     M       Student
   B        11     M       Student
   C        12     F       Student
+4
source share
2 answers

readLines, , read.csv:

# txt <- readLines("my_data.txt") # with a real data file
txt <- readLines(textConnection("Name,Age,Gender,Occupation A,10,M,Student B,11,M,Student C,11,F,Student"))

read.csv(text=gsub(" ","\n",txt))

  Name Age Gender Occupation
1    A  10      M    Student
2    B  11      M    Student
3    C  11      F    Student
+11

, , , , data.table fread read.csv, R, sed , R.

, CSV, /tmp/x.csv, - :

> data.table::fread("sed 's/ /\\n/g' /tmp/x.csv")
   Name Age Gender Occupation
1:    A  10      M    Student
2:    B  11      M    Student
3:    C  11      F    Student
+9

Source: https://habr.com/ru/post/1625460/


All Articles