Data and file structure

Data and File organization

Data and file structure

Data modes in R

All objects have a data mode, generally one of the modes below:

  • Numeric: a number value with decimal places.

  • Integer: a number value without decimal places

  • Complex: a number value that is considered to be an imaginary number (not commonly used). Character: a variable with values other than numbers or a single “.” value present.

  • Logical: a Boolean value of true or false.

  • Recursive objects: objects that have multiple pieces or perform actions, they have modes such as ‘list’ or ‘function’ or a few others.

Data classes in R

In R the object “class” determines which functions work with it. If code does not work, and you have checked all typos, you may need to check the object class. Maybe you are using the incorrect code for the class.

R can provide information on the type of object:

cellstocks <- read.csv("cellstocks.csv", header=T)
class(cellstocks)
[1] "data.frame"

Vector: a 1-dimensional object (e.g. column) of data with more than 1 cell. Can be any format (character, numeric, integer, factor, Boolean, date, etc.). If the vector only has one value, it is called a scaler.

Matrix: a 2-dimensional object (e.g. rows & columns) that only includes integer or numeric data and does not have row or column names.

Data frame: a simple 2-dimensional object (e.g. rows & columns) that can include any data format (character, numeric, integer, factor, Boolean, date, etc.) and has column and row names. R base can handle the manipulation of data frames

Data table: a more complex data frame, it is usually handled by different code than data frames.

List: a holder of multiple other objects (e.g. basket). Missing values

Data manipulation in R

Missing values

Missing values are a common issue in large data sets. Missing values are sometimes indicated by a blank, but they can also be indicated by N/A, NA or sometimes a zero or -9999. In the previous section na = c(“N/A”, “NA”, ““) was used to represent all missing values as NA. Alternatively, na.strings() can be used to achieve the same. If the na.strings argument is not defined, the default in R is to consider blank spaces as NA.

data1 <- read.csv("cellstocks.csv", header=T, na.strings =c("",".", "N/A","NA"), skip=5)

To identify the location of missing values within vectors, lists, matrices and data frames use the is.na() function which will return a logical vector with TRUE in the element locations that contain missing values represented by NA.

is.na(data1) 
      SampleID CellLine Origin vials    DT  rack Cell.Type...Source
 [1,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
 [2,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
 [3,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
 [4,]    FALSE    FALSE  FALSE  TRUE FALSE FALSE              FALSE
 [5,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
 [6,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
 [7,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
 [8,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
 [9,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[10,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[11,]    FALSE    FALSE  FALSE FALSE  TRUE FALSE              FALSE
[12,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[13,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[14,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[15,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[16,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[17,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[18,]    FALSE    FALSE  FALSE FALSE  TRUE FALSE              FALSE
[19,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[20,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[21,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[22,]    FALSE    FALSE  FALSE  TRUE FALSE FALSE              FALSE
[23,]    FALSE    FALSE  FALSE  TRUE FALSE FALSE              FALSE
[24,]    FALSE    FALSE  FALSE  TRUE FALSE FALSE              FALSE
[25,]    FALSE    FALSE  FALSE  TRUE FALSE FALSE              FALSE
[26,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[27,]    FALSE    FALSE  FALSE  TRUE FALSE FALSE              FALSE
[28,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[29,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[30,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[31,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[32,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[33,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[34,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[35,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[36,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[37,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[38,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[39,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE
[40,]    FALSE    FALSE  FALSE FALSE FALSE FALSE              FALSE

If I wanted to know in which rows the doubling time (DT) is missing, the which function can be used. It should return two row numbers.

which (is.na(data1$DT))
[1] 11 18

We can exclude missing values from mathematical calculations by using the na.rm=TRUE argument.

mean(data1$DT)
[1] NA
mean(data1$DT, na.rm=TRUE)
[1] 32.84211

Note that the NAs are not removed from the table, they are just skipped over, to calculate the mean.

To remove the NAs, a subset can be created. To sub-set data by omitting NA values the na.omit() function can be used. For our data tables this will remove all rows where a NA is present in any column.

data2 <- na.omit(data1) 

You might want to be more selective, and only remove rows with NAs in the DT column:

data3 <- data1[!is.na(data1$DT), ]

In data2, 8 rows were removed, in data3 only 2 rows were removed.

  • The ! in [!is.na(data1$DT) stands for the logical NOT.

  • The trailing comma inside Data1[!is.na(Data1$DT), ] is required because standard R data.frames require two dimensions to index: [rows, columns].

It is good pracitce to get into the habit of looking at your data modes and ensuring everything is as intended prior to executing an analysis. Rstudio will provide information on object structure int he top right panel. Alternatively, you can use the str function

str(data1)
'data.frame':   40 obs. of  7 variables:
 $ SampleID          : int  1 2 3 4 5 6 7 8 9 10 ...
 $ CellLine          : chr  "HeLa" "HEK293" "A549" "MCF-7" ...
 $ Origin            : chr  "Human" "Human" "Human" "Human" ...
 $ vials             : int  4 5 7 NA 5 6 12 24 1 2 ...
 $ DT                : int  24 30 24 40 34 60 40 30 22 40 ...
 $ rack              : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Cell.Type...Source: chr  "Cervical adenocarcinoma" "Embryonic kidney" "Lung carcinoma (alveolar type II)" "Breast adenocarcinoma ($ER^+$)" ...
str(data3)
'data.frame':   38 obs. of  7 variables:
 $ SampleID          : int  1 2 3 4 5 6 7 8 9 10 ...
 $ CellLine          : chr  "HeLa" "HEK293" "A549" "MCF-7" ...
 $ Origin            : chr  "Human" "Human" "Human" "Human" ...
 $ vials             : int  4 5 7 NA 5 6 12 24 1 2 ...
 $ DT                : int  24 30 24 40 34 60 40 30 22 40 ...
 $ rack              : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Cell.Type...Source: chr  "Cervical adenocarcinoma" "Embryonic kidney" "Lung carcinoma (alveolar type II)" "Breast adenocarcinoma ($ER^+$)" ...

If there is an error in a row or column, data modes can be changed. A few example:

data1$DT=as.numeric(data1$DT)

data1DT=as.character(data1$DT)

Re-coding values

Sometimes it is better to recode missing values to zero, in this case only in column “vials”. This can be done as follows:

data3$vials [is.na(data3$vials)]<-0
data3
   SampleID   CellLine                 Origin vials DT rack
1         1       HeLa                  Human     4 24    1
2         2     HEK293                  Human     5 30    1
3         3       A549                  Human     7 24    1
4         4      MCF-7                  Human     0 40    1
5         5 MDA-MB-231                  Human     5 34    1
6         6      HepG2                  Human     6 60    1
7         7     Caco-2                  Human    12 40    1
8         8      HT-29                  Human    24 30    1
9         9     HCT116                  Human     1 22    1
10       10       PC-3                  Human     2 40    1
12       12    U-87 MG                  Human     8 36    2
13       13    SH-SY5Y                  Human    94 70    2
14       14       U2OS                  Human     6 32    2
15       15       A431                  Human     2 36    2
16       16     PANC-1                  Human     7 28    2
17       17    SK-OV-3                  Human     8 40    2
19       19     Jurkat                  Human     4 26    2
20       20       K562                  Human     7 30    3
21       21      THP-1                  Human     9 45    3
22       22      HL-60                  Human     0 36    3
23       23       Raji                  Human     0 24    3
24       24       U937                  Human     0 32    3
25       25      Daudi                  Human     0 36    3
26       26    NIH/3T3                  Mouse     6 26    3
27       27        CHO        Chinese Hamster     0 24    3
28       28       Vero   African Green Monkey     0 30    3
29       29      COS-7   African Green Monkey     1 48    3
30       30       MDCK                    Dog     3 24    4
31       31     BHK-21           Baby Hamster    10 18    4
32       32  RAW 264.7                  Mouse     6 15    4
33       33   MC3T3-E1                  Mouse     6 36    4
34       34      C2C12                  Mouse     5 20    4
35       35     3T3-L1                  Mouse     2 24    4
36       36       PC12                    Rat     8 48    5
37       37      HaCaT                  Human     7 30    5
38       38      HUVEC                  Human     4 36    5
39       39        Sf9 Insect (S. frugiperda)     3 30    5
40       40  High Five         Insect (T. ni)    10 24    5
                     Cell.Type...Source
1               Cervical adenocarcinoma
2                      Embryonic kidney
3     Lung carcinoma (alveolar type II)
4        Breast adenocarcinoma ($ER^+$)
5         Triple-negative breast cancer
6              Hepatocellular carcinoma
7             Colorectal adenocarcinoma
8             Colorectal adenocarcinoma
9                  Colorectal carcinoma
10 Prostate adenocarcinoma (metastatic)
12              Glioblastoma multiforme
13                        Neuroblastoma
14                         Osteosarcoma
15                 Epidermoid carcinoma
16     Pancreatic ductal adenocarcinoma
17               Ovarian adenocarcinoma
19                T-lymphocyte leukemia
20   Chronic myelogenous leukemia (CML)
21             Acute monocytic leukemia
22               Promyelocytic leukemia
23      Burkitt's lymphoma B-lymphocyte
24                 Histiocytic lymphoma
25     Burkitt's lymphoma B-lymphoblast
26                 Embryonic fibroblast
27                     Ovary epithelial
28                    Kidney epithelial
29        Kidney fibroblast-like (SV40)
30                    Kidney epithelial
31                    Kidney fibroblast
32                  Monocyte/macrophage
33                       Pre-osteoblast
34                             Myoblast
35                        Pre-adipocyte
36                     Pheochromocytoma
37            Immortalized keratinocyte
38     Umbilical vein endothelial cells
39                        Ovarian cells
40                        Ovarian cells