0

I have multiple raw data sets from an older program and machine that I was using. Each data set has three numeric variables and about 90 observations, but the program separates the variables with commas instead of spaces. This causes r to read it as one long variable, when I want to read them as three separate variables while still keeping the three labels in the header.

Here's an example of one of the data sets

>dput(head(data)) structure(list(run.size.percent = structure(c(2L, 13L, 24L, 35L, 46L, 57L), .Label = c(",2000,", "1,0.375,0.013", "10,0.868,0.11", "11,0.953,0.12", "12,1.047,0.12", "13,1.149,0.13", "14,1.261,0.14", "15,1.385,0.14", "16,1.520,0.15", "17,1.668,0.15", "18,1.832,0.16", "19,2.011,0.17", "2,0.412,0.023", "20,2.207,0.17", "21,2.423,0.18", "22,2.660,0.19", "23,2.920,0.20", "24,3.205,0.21", "25,3.519,0.22", "26,3.863,0.24", "27,4.240,0.25", "28,4.655,0.26", "29,5.110,0.28", "3,0.452,0.034", "30,5.610,0.30", "31,6.158,0.31", "32,6.760,0.33", "33,7.421,0.35", "34,8.147,0.37", "35,8.943,0.39", "36,9.817,0.42", "37,10.78,0.45", "38,11.83,0.47", "39,12.99,0.50", "4,0.496,0.049", "40,14.26,0.53", "41,15.65,0.56", "42,17.18,0.58", "43,18.86,0.59", "44,20.70,0.59", "45,22.73,0.58", "46,24.95,0.55", "47,27.39,0.52", "48,30.07,0.49", "49,33.01,0.46", "5,0.545,0.061", "50,36.24,0.45", "51,39.78,0.45", "52,43.67,0.45", "53,47.94,0.44", "54,52.62,0.42", "55,57.77,0.38", "56,63.41,0.35", "57,69.61,0.32", "58,76.42,0.31", "59,83.89,0.33", "6,0.598,0.072", "60,92.09,0.36", "61,101.1,0.42", "62,111.0,0.49", "63,121.8,0.59", "64,133.7,0.74", "65,146.8,0.94", "66,161.2,1.19", "67,176.9,1.49", "68,194.2,1.82", "69,213.2,2.18", "7,0.656,0.083", "70,234.1,2.55", "71,256.9,2.94", "72,282.1,3.34", "73,309.6,3.78", "74,339.9,4.25", "75,373.1,4.73", "76,409.6,5.20", "77,449.7,5.60", "78,493.6,5.87", "79,541.9,5.93", "8,0.721,0.093", "80,594.9,5.77", "81,653.0,5.37", "82,716.8,4.77", "83,786.9,4.03", "84,863.9,3.21", "85,948.3,2.36", "86,1041,1.55", "87,1143,0.81", "88,1255,0.30", "89,1377,0.056", "9,0.791,0.10", "90,1512,0.0044", "91,1660,0", "92,1822,0"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")

Any ideas on a quick way to resolve this? Is there a line of code that I can throw in after I read the data sets in r? Thanks for any help!

Tom
  • 377
  • 3
  • 13
  • 1
    This is a duplicate. Try `read.table(text = as.character(data[[1]]), sep = ",")`. – Rui Barradas Jul 12 '19 at 16:36
  • ```tidyr::separate(names(df), unlist(strsplit(names(df), "\\.")), ",", data=df)``` – M-- Jul 12 '19 at 16:40
  • @RuiBarradas dupe-target does not talk about preserving the column names. Maybe there's a better one out there? – M-- Jul 12 '19 at 16:42
  • 1
    @RuiBarradas do you know if there's a way to do it without making the column names turn to V1, V2, and V3? It would be easier to do the analysis if the names were at they were. I will edit my question to reflect this – Tom Jul 12 '19 at 16:53
  • Your dataframe is not called `df`. You have it as `data`. Then my answer would change to: ```tidyr::separate(names(data), unlist(strsplit(names(data), "\\.")), ",", data=data)``` – M-- Jul 12 '19 at 16:56
  • Oh wow, I probably should have caught that. That worked perfectly, thanks! – Tom Jul 12 '19 at 17:07
  • @M-M The dupe is in his turn a dupe, and some of the answers set names other than `"Vn"`. – Rui Barradas Jul 12 '19 at 17:52

0 Answers0