为大型列表中的每个元素自动在大型列表中创建矢量的功能 [英] Function to automatically create vector in a large list for each element of the large list

查看:56
本文介绍了为大型列表中的每个元素自动在大型列表中创建矢量的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的数据框:

I have a single Dataframe with the following structure:

A.Data 是带有数字数据的向量

A.Data is a vector with numeric data

A.Quartile 是一个向量,用于计算每个 A.data 的四分位数,并且该数据属于哪个四分位数.(Q1,Q2,Q3,Q4).

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).

我使用了非常相似的代码来创建分位数和所属的Q.

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)

现在,(这是我的问题)我有一个导入到R中的.csv文件,其中包含超过400个带有XYZ.Data向量的元素

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

因此,当我将.csv文件导入到我的环境中时,我想创建一个函数来一次创建所有XYZ.Quartile向量,而我不知道如何.

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.

重点是使用功能从.csv文件读取加载到环境中的列表中的所有元素,并具有创建B.Quartile,C.Quartile,D.Quartile,向量的功能.列表中的每个元素.

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.

任何人都可以帮忙吗?

非常感谢您的任何评论.

Thank you very much for any comment.

quantile(x <- Orange$circumference)
Orange<- within(Orange, Quartile <- as.integer(cut(Orange$circumference, quantile(Orange$circumference, probs=0:4/4), include.lowest=TRUE)))

推荐答案

您的示例数据令人困惑.尚不清楚您的数据结构是什么,所以我只是假装您的列表是matrix/data.frame的列.

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"

编辑1 请参见以下代码:

# 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

这篇关于为大型列表中的每个元素自动在大型列表中创建矢量的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆