0

I have a single Dataframe with the following structure:

A.Data is a vector with numeric data

A.Quartile is a vector with the calculation of quartiles for each A.data and which quartile belongs to this data. (Q1,Q2,Q3,Q4).

I used a very similar code to create the quantile and the Q which belongs to.

quantile(x <- rnorm(1001))
list2env(setNames(as.list(quantile(x <- rnorm(1001))),paste0("Q",1:5)),.GlobalEnv)

enter image description here

Now, ( and here is my problem) I have a .csv that I imported into R, with more than 400 elements with XYZ.Data vectors

So when I imported the .csv file into my environment, I would like to create a function to create in one time all the XYZ.Quartile vectors and I don't know how.

The point would be to read all elements in my list loaded into environment from a .csv file with a function and have the function to create the B.Quartile,C.Quartile,D.Quartile, vectors... one for each element in the list.

Anyone can help please?

Large list with 400 elements

Thank you very much for any comment.

PD: New Code Example

quantile(x <- Orange$circumference)
Orange<- within(Orange, Quartile <- as.integer(cut(Orange$circumference, quantile(Orange$circumference, probs=0:4/4), include.lowest=TRUE)))
Community
  • 1
  • 1
Rick
  • 171
  • 1
  • 13
  • 2
    Albert, can you give a [minimal, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (no pictures of your data!) and expected output? – markus Sep 23 '18 at 14:22
  • Hi Markus, I have edited the question and added the code to calculate the quartiles and the Qs, any help in how to convert it into a function to: 1-read my environtment in order to identify each element imported from .csv, 2-Calculate the Quantiles for each element and assign it to its corresponding Q(1,2,3,4)? Thanks! – Rick Sep 23 '18 at 14:29

1 Answers1

2

Your example data is confusing. It's not clear what the structure of your data is so I'm just pretending your lists are columns of a matrix/data.frame.

# proper example data
set.seed(1)
dat <- replicate(6, rnorm(20))
colnames(dat) <- LETTERS[1:6]
head(dat)
#              A           B          C           D          E           F
#[1,] -0.6264538  0.91897737 -0.1645236  2.40161776 -0.5686687 -0.62036668
#[2,]  0.1836433  0.78213630 -0.2533617 -0.03924000 -0.1351786  0.04211587
#[3,] -0.8356286  0.07456498  0.6969634  0.68973936  1.1780870 -0.91092165
#[4,]  1.5952808 -1.98935170  0.5566632  0.02800216 -1.5235668  0.15802877
#[5,]  0.3295078  0.61982575 -0.6887557 -0.74327321  0.5939462 -0.65458464
#[6,] -0.8204684 -0.05612874 -0.7074952  0.18879230  0.3329504  1.76728727

# for each column i
qdat <- apply(dat, 2, function(i){
  q <- quantile(i)
  # for each element j in column i
  sapply(i, function(j){
    paste0("Q",1:5)[sum(j > q)+1]
  })
})
head(qdat)
#     A    B    C    D    E    F   
#[1,] "Q2" "Q5" "Q3" "Q5" "Q2" "Q2"
#[2,] "Q3" "Q5" "Q3" "Q3" "Q3" "Q4"
#[3,] "Q2" "Q4" "Q5" "Q5" "Q5" "Q1"
#[4,] "Q5" "Q1" "Q4" "Q3" "Q1" "Q4"
#[5,] "Q3" "Q4" "Q2" "Q2" "Q4" "Q2"
#[6,] "Q2" "Q3" "Q2" "Q4" "Q4" "Q5"

EDIT 1 See the following code:

# example data
set.seed(1)
dat <- replicate(3, rnorm(20))
colnames(dat) <- paste0(LETTERS[1:3],".Data")

replacewithQ <- function(x) {
  as.integer(cut(x, 
                 quantile(x, 
                          probs=0:4/4), 
                 include.lowest=TRUE)
  )
}

qdat <- apply(dat, 2, replacewithQ)
colnames(qdat) <- gsub("Data","Quartile",colnames(dat))
newdat <- cbind(dat, qdat)
head(newdat)
#         A.Data      B.Data     C.Data A.Quartile B.Quartile C.Quartile
#[1,] -0.6264538  0.91897737 -0.1645236          1          4          2
#[2,]  0.1836433  0.78213630 -0.2533617          2          4          2
#[3,] -0.8356286  0.07456498  0.6969634          1          3          4
#[4,]  1.5952808 -1.98935170  0.5566632          4          1          3
#[5,]  0.3295078  0.61982575 -0.6887557          2          3          1
#[6,] -0.8204684 -0.05612874 -0.7074952          1          2          1
Evan Friedland
  • 3,062
  • 1
  • 11
  • 25
  • Hi, Evan thanks for your response. I've added new code in the original question (bottom) with a built-in example and you can run the code and get the column I'd like to spread all over the elements of my list. The list comes from a .csv imported into R and it is in my environment, so I think the difficult for me is to convert it into a function that read all the element names of the list and create the new column, called element1.Quartile, element2.quartile, element3,quartile, etc,etc,etc in the same way that in the Orange sample. Thanks for any help and sorry for my confusing example. ;-) – Rick Sep 23 '18 at 15:59
  • 1
    It's not clear at all still - you're new example only shows me you want the output to be integers instead of the strings I've posted above. Is your "list" a list like `list(A = rnorm(3), B = rnorm(6), C = rnorm(4))` or is it like `data.frame(A = rnorm(4), B = rnorm(4), C = rnorm(4))` ? can you provide the out for `str(your_csv_list_object)` in your environment? Or make an example list that actually carries over instead of using a single column from orange and calling it your list? Unless your data is in fact 1 single vector of data, this will not give you useful responses. – Evan Friedland Sep 23 '18 at 16:07
  • $ Element1 :An ‘xts’ object on 2018-01-02/2018-09-21 containing: Data: num [1:183, 1:9] 157 159 161 161 162 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:9] "Elem1.A" "Elem1.B" "Elem1.C" "Elem1.D" ... Indexed by objects of class: [Date] TZ: UTC Thanks for your patience. – Rick Sep 23 '18 at 16:16
  • Your Edit works fine. Last question I have is, Is it possible to create the new column directly to the list without creating new Dataframes? I mean a function that read element names, create a new column with the elementname.Qdata and spread all over the current list? Thank you very much for your response. – Rick Sep 23 '18 at 17:18
  • 1
    @Albert a function() that’s essentially what I am doing by usind cbind and the column renaming? – Evan Friedland Sep 24 '18 at 06:45