简体   繁体   English

如何创建一个从数据框中提取列并使用R创建另一个函数的函数?

[英]How to create a function to extract columns from a data frame and create another using R?

I need to create a function to make my workflow better. 我需要创建一个函数来改善我的工作流程。 I already create a code, but I need to repeat several times and change the number according to the mat[i] columns. 我已经创建了一个代码,但是我需要重复几次并根据mat[i]列更改数字。 I mean, if I'm going to create another matrix for mat2 , I need to replace in all my code, the number 1 to 2 , for mat3 : 2 to 3 , for mat4 : 4 to 5 . 我的意思是,如果我要创建另一个矩阵mat2 ,我需要在我所有的代码来替换,数字12 ,对于mat323 ,为mat445

I have the data frame below (it is not my real data): 我下面有数据框(不是我的真实数据):

year <- c(rep(1998:2001, 4))
Age <- c(rep(15:18, 4))
mat1 <- c(rep(0.01, 16))
mat2 <- c(rep(0.012, 16))

df <- data.frame(year, Age, mat1, mat2)

I need to create n final numeric matrices, one for each mat(i) , (i = 1, 2,...,n). 我需要创建n最终的数值矩阵,每个mat(i) ,(i = 1,2,...,n)一个。 The rows would be the age and in the columns year . 行将是age ,列将是year

I got the result that I wanted using the code below: 我得到了想要使用以下代码的结果:

library(dplyr)
mat1 <- #selecting just intensities of order 1 and creating matrices
  dplyr::select(df, Age, year, mat1) #choosig the variables that I want to keep %>% 
  spread(year, mat1)

names(mat1)[c(2:5)] <- paste0("year ", names(mat1[2:5])) #change colnames as it is in the model
mat1[ ,1] <- paste0("age ", mat1[,1]) #alter the row from column "age"

mat_oe1 <- data.matrix(mat1[2:5])
dimnames(mat_oe1) <- list(c(mat1[,1]), #row names
                        c(names(mat1[2:5])))#columns names

#Saving as txt to read in the model later
write.table(mat_oe1, file = "mat_oe1.txt", sep = "\t",
            row.names = T, col.names = T)

The final result (which must be equal this to be inserted in a model that is already created) that I got is for mat1 . 我得到的最终结果(必须等于要插入到已经创建的模型中的结果)是针对mat1 For example, this result below it is for all the results for the variable mat1 : 例如,下面的结果适用于变量mat1所有结果:

view(mat1)
        year 1998  year 1999  year 2000  year 2001
age 15   0.01        0.01       0.01       0.01
age 16   0.01        0.01       0.01       0.01
age 17   0.01        0.01       0.01       0.01
age 18   0.01        0.01       0.01       0.01


view(mat2)
        year 1998  year 1999    year 2000    year 2001
age 15   0.012        0.012       0.012       0.012
age 16   0.012        0.012       0.012       0.012
age 17   0.012        0.012       0.012       0.012
age 18   0.012        0.012       0.012       0.012

Is this what you need 这是你需要的吗

library(reshape2)
df$mat <- df$mat1+df$mat2
dcast(df,Age~year,value.var = "mat",sum)

EDIT 编辑

#df that you provided
structure(list(year = c(1998L, 1999L, 2000L, 2001L, 1998L, 1999L, 
2000L, 2001L, 1998L, 1999L, 2000L, 2001L, 1998L, 1999L, 2000L, 
2001L), Age = c(15L, 16L, 17L, 18L, 15L, 16L, 17L, 18L, 15L,   
16L, 17L, 18L, 15L, 16L, 17L, 18L), mat1 = c(0.1, 0.1, 0.1, 0.1, 
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1), 
mat2 = c(0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 
0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12)), .Names = c("year", 
"Age", "mat1", "mat2"), row.names = c(NA, -16L), class = "data.frame")    

first seperate the df into two dataframe by column which contains mat and no mat 首先将df按列分为两个数据帧,其中包含matno mat

library(reshape2)
library(zoo)

df_no_mat <-  df[ , -which(names(df) %in% grep("mat", names(df), value = TRUE))]
#df_no_mat OUTPUT head
year    Age
1998    15
1999    16
2000    17
2001    18
1998    15
1999    16

df_mat <-  df[ , which(names(df) %in% grep("mat", names(df), value = TRUE))]
#df_mat OUTPUT head
mat1    mat2
 0.1    0.12
 0.1    0.12
 0.1    0.12
 0.1    0.12
 0.1    0.12
 0.1    0.12

Loop and spread which saves the df in a list 循环传播,将df保存在列表中

datalist = list()

for(i in names(df_mat)){
    df_no_mat$i <- df_mat[,i]
    datalist[[i]] <- dcast(df_no_mat,Age~paste0('year ',year),value.var = "i",mean)
    datalist[[i]] <- na.aggregate(datalist[[i]]) 
}

Access you df created for mats by using datalist$mat1 or datalist[[1]] and datalist$mat2 or datalist[[2]] 使用datalist$mat1 or datalist[[1]]datalist$mat2 or datalist[[2]]访问为df创建的垫

#Output
$mat1
Age year 1998   year 1999   year 2000   year 2001
15  0.01    0.01    0.01    0.01
16  0.01    0.01    0.01    0.01
17  0.01    0.01    0.01    0.01
18  0.01    0.01    0.01    0.01

$mat2
Age year 1998   year 1999   year 2000   year 2001
15  0.012   0.012   0.012   0.012
16  0.012   0.012   0.012   0.012
17  0.012   0.012   0.012   0.012
18  0.012   0.012   0.012   0.012

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 R 中的 function 创建包含行和列的数据框? - how to create a data frame with rows and columns for a function in R? 如何用R中的csv数据创建带有列的帧数据结构? - how to create the frame data structure with columns from csv data in R? 如何从具有 3 列的数据框在 R 中创建数组? - How Do I Create an Array in R from a Data Frame with 3 Columns? 根据 R 中的列名创建一个包含来自另一个数据框中的列的新数据框 - create a new data frame with columns from another data frame based on column names in R 从R数据框中的多个列中提取数据,然后搜索另一个 - Extract data from multiple columns in R data frame, then searching another R - 用于创建包含来自另一个data.frame的操纵数据的data.frame的函数 - R - Function to create a data.frame containing manipulated data from another data.frame 根据来自另一个数据框的行创建数据框的列 - Create columns of data frame based on rows from another data frame 从R中的函数创建数据框 - Create data frame from function in R 如何在R数据帧中的列之间循环并在每次迭代中使用列名创建新的数据帧? - How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration? 如何在R中使用数据框创建求和函数? - How to create a summation function with data frame in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM