-3

I am trying to make a data table in R, where there is a heading with variables and a subheading with 4 "reps" grouped to each variable..

Here is a result of View(my.data) which was read from csv-file by my.data <- read.csv("DataTable.csv", header=T, skip=1):

enter image description here

How do I do this? It does not look right at this moment...

Artem
  • 3,304
  • 3
  • 18
  • 41
Tine
  • 1
  • I loaded the data as: my.data <- read.table("DataTable.csv", header=T, sep=",") – Tine Sep 05 '18 at 13:38
  • 3
    Welcome to SO! Please read [ask] and give a [mcve]! From images no one can reproduce your data! – jogo Sep 05 '18 at 13:39
  • 5
    "R is a free, open-source programming language and software environment for statistical computing, bioinformatics, visualization and general computing. **Provide minimal, reproducible, representative example(s) with your questions. Use dput() for data and specify all non-base packages with library calls. Do not embed pictures for data or code, use indented code blocks.** For statistics questions, use stats.stackexchange.com" – Andre Elrico Sep 05 '18 at 13:39
  • Hi Tine! Maybe try `my.data <- read.csv("DataTable.csv", header=T, skip=1)` – C. Braun Sep 05 '18 at 13:40
  • Unfortunately it did not help. It just removed the first header, where the variables are supposed to be – Tine Sep 05 '18 at 13:50
  • 1
    Could you provide a part of your csv? It is unlikely anyone can help you here unless you provide a little more information. – C. Braun Sep 05 '18 at 14:09
  • Based on your screenshot, perhaps your csv isn't actually comma-separated? But we can't verify without more information from you. – Z.Lin Sep 06 '18 at 02:16
  • I realised that the CSV file is separated by "space". How do I tell R that.. sep=""? – Tine Sep 09 '18 at 08:03

1 Answers1

2

As your CSV file contains a two-line header, you have to load the data and the header separately. Moreover you should take into consideration what a separation character is. In your case, it is space, not comma. Please see the simulation and suggested CSV file reading procedure below.

    # csv - simulation
set.seed(123)
h1 <- c("", "", "WiId 24h", "", "", "", "WiId 56h", "", "", "")
h2 <- c("Match" ,"ID" ,"Rep1" ,"Rep2" ,"Rep3" ,"Rep4" ,"Rep1" ,"Rep2","Rep3" ,"Rep4")

h1_w <- paste0(h1, sep = " ", collapse = "")
h2_w <- paste0(h2, sep = " ", collapse = "")
df <- data.frame(1:10, 0:9, matrix(rnorm(10 * 8), ncol =  8))
df2 <- rbind(h2, df)
names(df2) <- h1
write.table(df2, file = "test2.csv", sep = " ", row.names = FALSE)

# reading of 2-line csv
header <- read.table("test2.csv", sep = " ", header = TRUE, nrows = 1)
body <- read.table("test2.csv", sep = " ", header = FALSE, skip = 2)

header
# header is:
# X X.1 WiId.24h  X.2  X.3  X.4 WiId.56h  X.5  X.6  X.7
# 1 Match  ID     Rep1 Rep2 Rep3 Rep4     Rep1 Rep2 Rep3 Rep4

head(body)
# body is:
#   V1 V2          V3         V4         V5         V6         V7          V8         V9        V10
# 1  1  0 -0.56047565  1.2240818 -1.0678237  0.4264642 -0.6947070  0.25331851  0.3796395 -0.4910312
# 2  2  1 -0.23017749  0.3598138 -0.2179749 -0.2950715 -0.2079173 -0.02854676 -0.5023235 -2.3091689
# 3  3  2  1.55870831  0.4007715 -1.0260044  0.8951257 -1.2653964 -0.04287046 -0.3332074  1.0057385
# 4  4  3  0.07050839  0.1106827 -0.7288912  0.8781335  2.1689560  1.36860228 -1.0185754 -0.7092008
# 5  5  4  0.12928774 -0.5558411 -0.6250393  0.8215811  1.2079620 -0.22577099 -1.0717912 -0.6880086
# 6  6  5  1.71506499  1.7869131 -1.6866933  0.6886403 -1.1231086  1.51647060  0.3035286  1.0255714
Artem
  • 3,304
  • 3
  • 18
  • 41