jypst2003
jypst2003

Reputation: 131

Error in : incorrect number of dimensions

I wrote a code to sort my large data set. I tested my code for one file (e.g. "K_7-1_0H.TXT"), it worked totally fine without any warning and error message. I got the correct .csv file that I want. But when I ran the loop, the error message came:

Error in `[.default`(data, 1, ) : incorrect number of dimensions 

Here it is my code:

kcl <- list.files(pattern = "K_.*\\.TXT",recursive = TRUE)
c <- c(0, 0.001,0.01,0.05,0.25,0.5,1.0,1.5,2.0,2.5,3.0,3.4)
Kconc. <- rep(c,each=8)
for (i in kcl) {
  data <- read.csv(i,header=FALSE,sep = ",")
  data <- data[-c(1:14),] #delete noise info
  colnames(data) <- as.character(unlist(data[1,])) #add column name
  data <- data[-1,]
  data <- data[,-1]
  #change cell number and sort
  data$`Well Label` <- as.character(data$`Well Label`)
  data[1,1] <- "T01"
  data[12,1] <- "T02"
  ...
  data[89,1] <- "T09"
  data <- arrange(data, `Well Label`) #sort according to table number
  data <- data[-1,] #delete noise info
  data <- cbind(data,Kconc.)
  j <- sub("\\.[[:alnum:]]+$","",i) #grep the isolate name without the extention
  write.csv(data, paste0(j,".csv"))
}

here is the list content"

> kcl
 [1] "K_10-1_0.TXT" "K_10-3_0.TXT"
 [9] "K_10-3_6.TXT" "K_10-3_7.TXT" "K_11-1_8.TXT"
[17] "K_11-2_0.TXT" "K_11-3_8.TXT"
[25] "K_7-1_0H.TXT" "K_7-3_82.TXT" "K_8-1_0H.TXT" "K_8-1_60.TXT" "K_8-1_72.TXT" "K_8-1_84.TXT"
[49] "K_9-1_0Z.TXT" "K_9-1_60.TXT" "K_9-1_72.TXT" "K_9-1_84.TXT" "K_9-2-84.TXT" 

When I checked my files, files like "K_10*.csv" and "K_11*.csv" were created and I got what I want, but files like "K_7*.TXT", "K_8*.TXT" and "K_9*.TXT" didn't work at all, which means I didn't even create the .csv for these files.

I don't really understand the error message and why the code only works for some files. Could somebody help me?


Edit: input and expecting output
Inputs are .txt file as follow:

[Assay],C:\REVEL\650-S.ASY
"Assay title",Untitled Assay
"Read Time",11.04.17,13:04:00
"Operator",
"Comments",
"Kit Lot Number",,
"Wells",A1 - H12
OD RESULTS
"Units",OD

[Results],Results are sorted on Sample ID,in ascending order

"Sample ID","Well Label","OD Results"
"T1","T1",0.045
"T10","T10",0.044
"T11","T11",0.045
"T2","T2",0.045

Expected output:

    Well Label  OD Results  Hconc.
2   T01 0.189   0
3   T02 0.11    0
4   T03 0.151   0
5   T04 0.053   0

Upvotes: 1

Views: 6332

Answers (1)

zx8754
zx8754

Reputation: 56004

Try this example:

library(dplyr)

# skip info rows
df1 <- read.csv("test.txt", skip = 12, stringsAsFactors = FALSE)
Kconc <- c(0, 0.001,0.01,0.05,0.25,0.5,1.0,1.5,2.0,2.5,3.0,3.4)

# Prerix with zero, e.g.: T1 to T01, then sort
res <- 
  df1 %>% 
  transmute(
    `Well Label` = if_else(nchar(df1$Sample.ID) == 2,
                           paste0(substr(df1$Sample.ID, 1, 1),
                                  0,
                                  substr(df1$Sample.ID, 2, 3)),
                           df1$Sample.ID),
    `OD Results` = OD.Results) %>% 
  arrange(`Well Label`)
res
#   Well Label OD Results
# 1        T01      0.045
# 2        T02      0.045
# 3        T10      0.044
# 4        T11      0.045

Then cbind Kconc, no need for rep as it recycles. In this example we have only 4 rows, so to get correct result for this example we need to use res <- cbind(res, Kconc[1:4]).

res <- cbind(res, Kconc)

Also, read about natural order from gtools:

  df1[ gtools::mixedorder(df1$Sample.ID), ]
#   Sample.ID Well.Label OD.Results
# 1        T1         T1      0.045
# 4        T2         T2      0.045
# 2       T10        T10      0.044
# 3       T11        T11      0.045

test.txt

[Assay],C:\REVEL\650-S.ASY
"Assay title",Untitled Assay
"Read Time",11.04.17,13:04:00
"Operator",
"Comments",
"Kit Lot Number",,
"Wells",A1 - H12
OD RESULTS
"Units",OD

[Results],Results are sorted on Sample ID,in ascending order

"Sample ID","Well Label","OD Results"
"T1","T1",0.045
"T10","T10",0.044
"T11","T11",0.045
"T2","T2",0.045

Upvotes: 1

Related Questions