[英]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
,我需要在我所有的代码来替换,数字1
到2
,对于mat3
: 2
至3
,为mat4
: 4
至5
。
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
按列分为两个数据帧,其中包含mat
和no 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.