Issues to separate a colum

Hello! I'm a data science student and I'm trying to tidy some data I got from an xlsx file. I was able to import the file to rstudio but now, I have no idea on how to tidy the data. I'd like to keep 2 columns: 1 for the CVEs and another for the hosts. I've tried many different tutorials from the web to no avail. I attached a picture of where I'm stuck. Thanks for the help.

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

Like this:

",", "CVE-2020-1091,CVE-2020-0718,CVE-2020-0912,CVE-2020-0836,CVE-2020-1508,CVE-2020-0838,CVE-2020-0921,CVE-2020-1559,CVE-2020-0922,CVE-2020-0761,CVE-2020-1256,CVE-2020-1598,CVE-2020-0664,CVE-2020-1038,CVE-2020-1115,CVE-2020-1039,CVE-2020-1252,CVE-2020-1593,CVE-2020-1012,CVE-2020-0782,CVE-2020-1013,CVE-2020-1376,CVE-2020-1596,CVE-2020-1491,CVE-2020-1030,CVE-2020-1052,CVE-2020-1074,CVE-2020-1250,CVE-2020-1031,CVE-2020-1097,CVE-2020-0648,CVE-2020-1319,CVE-2020-1228,CVE-2020-0856,CVE-2020-0878,CVE-2020-0911,CVE-2020-1245,CVE-2020-1589,CVE-2020-0790,CVE-2020-1285,CVE-2020-1083,",
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,

Almost, you haven't posted the complete dput output, it is missing the initial part, please check again and post the complete output.

To post the data use
dput(data-name), then copy and paste the output.

It is gigantic! 11K lines. Can I use a pastebin website?

A few lines like in your screenshot would be enough

dput(head(data-name, 20))

Alternatively you can share a link to the raw xlsx file. Maybe this would be a better option since it seems you haven't correctly imported the data on the first place (one record is on the column header)

The xlsx is in fact a csv I transformed. Unfortunately, I can't share the document, it's a report from a client. I notice the record on the header, and yeah, I probably messed up a bit.

Then a few rows from the raw file opened as text would help us identify potential issues and maybe avoid the need for manually separating columns.

Since you are already showing about 20 records on the screenshot I don't think you would have further data security concerns if you share the same data as raw text.

Point taken. I'll do that tomorrow. The file is in my work laptop and today is a holiday here. Thanks for all the insights so far.

Is this enough? It just goes on and on like this for many pages.

That is a screenshot, unless someone is willing to manually type it from the image, it is of no use.

You could simply copy and paste here a few lines directly from your csv file, that way we can simply paste it in a text editor and have a small sample of your raw csv file.

Here is a rough example of what we need as a csv file.
"1",NA,"China","1,444,463,616","1,439,323,776","9,706,961 km²","149/km²","0.34%","18.34%",1
"2",NA,"India","1,394,095,436","1,380,004,385","3,287,590 km²","424/km²","0.97%","17.69%",2
"3",NA,"United States","333,014,395","331,002,651","9,372,610 km²","36/km²","0.58%","4.23%",3
"4",NA,"Indonesia","276,507,330","273,523,615","1,904,569 km²","145/km²","1.04%","3.51%",4
"5",NA,"Pakistan","225,419,269","220,892,340","881,912 km²","255/km²","1.95%","2.86%",5
"6",NA,"Brazil","214,066,360","212,559,417","8,515,767 km²","25/km²","0.67%","2.72%",6
"7",NA,"Nigeria","211,670,609","206,139,589","923,768 km²","229/km²","2.55%","2.68%",7
"8",NA,"Bangladesh","166,385,502","164,689,383","147,570 km²","1,127/km²","0.98%","2.11%",8
"9",NA,"Russia","145,910,977","145,934,462","17,098,242 km²","9/km²","-0.02%","1.85%",9
"10",NA,"Mexico","130,330,309","128,932,753","1,964,375 km²","66/km²","1.03%","1.65%",10
"11",NA,"Japan","126,029,236","126,476,461","377,930 km²","334/km²","-0.34%","1.60%",11
"12",NA,"Ethiopia","118,025,144","114,963,588","1,104,300 km²","107/km²","2.53%","1.50%",12
"13",NA,"Philippines","111,122,158","109,581,078","342,353 km²","324/km²","1.34%","1.41%",13
"14",NA,"Egypt","104,356,694","102,334,404","1,002,450 km²","104/km²","1.88%","1.32%",14
"15",NA,"Vietnam","98,211,145","97,338,579","331,212 km²","296/km²","0.85%","1.25%",15

It should be possible for someone to paste this into text editor and then read it into R.

First: sorry for my being painfully dumb and slow.
Second: thank you very much for all the time and patience spent with me.
Third time is charm, right?


Is this what you're asking for? I hope so. :stuck_out_tongue:

Ok, now I can confirm that your csv file doesn't actually contain comma-separated values but a single column of character strings so manual separation of the data is mandatory.
I would do it this way. Notice that I don't know what criteria to use to fill the missing CVE values so I left them as NA, I would need domain-specific knowledge of the data to figure that out.


sample_df <- read.csv("sample.csv", header = FALSE)

sample_df %>% 
    separate_rows(V1, sep = ",(?=CVE)") %>% 
    separate(V1, into = c("CVE", "IP"), sep = ",") %>% 
    fill(IP, .direction = "up")
#> # A tibble: 246 × 2
#>    CVE            IP           
#>    <chr>          <chr>        
#>  1 CVE-2004-2761
#>  2 CVE-2017-10081
#>  3 CVE-2017-10078
#>  4 CVE-2017-10111
#>  5 CVE-2017-10114
#>  6 CVE-2017-10135
#>  7 CVE-2017-10053
#>  8 CVE-2017-10074
#>  9 CVE-2017-10096
#> 10 CVE-2017-10110
#> # … with 236 more rows

Created on 2022-06-19 by the reprex package (v2.0.1)

Yeah, I don't get that, too. Apparently, that's how the tool creates de csv report. I've tried creating the report more than once, to no avail, the other options the tool provides (html and pdf) are too clogged and bloated for the data I need. Anyway, this will suffice I guess. I'll try to produce a new csv and open directly in R, this one I first opened in excel even though the tool says opening in excel could break the file. Thanks again for your patience and for pointing me in the right direction, I was doing everything wrong :stuck_out_tongue:

Close but no cigar.

How many column of data were you expecting? Currently i am trying to read the data in a couple of ways but I am ending up with a data frame and a tibble with 92 columns and 1 row of data which does not seem correct. How many column of data were you expecting?


dat1 <-   read_csv("scristiano1.csv",   col_names = FALSE)

dat3 <-   read.csv("scristiano1.csv", sep = ",", quote = '"',
                   header = FALSE)

I really do not what that those data are doing. Essentially the layout or formatting looks strange. The commas , and quotation marks " do not seem to make sense.

We have


enclosed by quotation marks but not followed by a comma but then we have this long series enclosed in quotation marks and each element separated within those quotation marks by a comma. By the way what is that? It does not conform to the pattern of the rest of the data. Oh, is that the the xlsx file name? How did it get in there?

Anyway, let's go back to your original xlsx screenshot. Line 1 has a large number of comma separated data points separated by commas. Are each data point in a separate cell or are 32 or 33 entries in a single cell?

"CVE-2017-10081,CVE-2017-10078,CVE-2017-10111,CVE-2017-10114,CVE-2017-10135,CVE-2017-10053,CVE-2017-10074,CVE-2017-10096,CVE-2017-10110,CVE-2017-10176,CVE-2017-10198,CVE-2017-10116,CVE-2017-10115,CVE-2017-10118,CVE-2017-10117,CVE-2017-10193,CVE-2  017-10090,CVE-2017-10101,CVE-2017-10145,CVE-2017-10067,CVE-2017-10089,CVE-2017-10243,CVE-2017-10125,CVE-2017-10102,CVE-2017-10086,CVE-2017-10121,CVE-2017-10087,CVE-2017-10109,CVE-2017-10108,CVE-2017-10105,CVE-2017-10104,CVE-2017-10107,"

The last value in the above does not match the CVE-9999-9999 pattern. Any particular reason?

Something I probably should have asked earlier, how was the data generated?
Is it possible that each row in the spreadsheet is a variable?

As andresrcs said some posts ago, the file doesn't contain comma separated values, it's a single column with character strings. That's how the tool generates a "csv" report and it even warns you the file will break if you open it in excel - and it actually does. Anyway, andresrcs solution worked like a charm, I still have to cope with the NA rows but now that it is tidy and organized, I exported it excel and can use filters to fill in the blank spaces with the missing CVEs.

You can definitely fill the missing CVE values from R, no need to go to excel for that, I didn't do it because I don't know what criteria to use to fill them. If you can explain what criteria you are using, I can help you to implement it in R.

I was thinking about doing it manually but now that I've taken a deeper look at the document, I realized there are too many blank spaces for the job. I'm still thinking about a strategy, I know the report in pdf has the complete info but as I've said before, the pdf is too bloated with other security info and differently from the csv, the tool doesn't allow me to select what info I want to include in the pdf report, it just gives me the full thing. I don't know what criteria to use because I don't know which cve is missing.