[英]How to create a function with already written for-loop to automatically create vectors in r environment
I have a df
with fixed columns and unfixed row number.我有一个带有固定列和不固定行号的df
。 I created empty vectors and populate R commands to create vectors on its own once I use eval(parse(text = someVector))
.我创建了空向量并填充了 R 命令以在我使用eval(parse(text = someVector))
后自行创建向量。 What I did with for-loop
works, but I would like to turn it into a function and/or use *apply()
and I don't know how to do that.我对for-loop
所做的工作有效,但我想将它变成一个函数和/或使用*apply()
但我不知道该怎么做。 I would very much like to upgrade my programming skills.我非常想提升我的编程技能。 I would like to be able to choose the variables by name or position and always go through every row.我希望能够按名称或位置选择变量并始终遍历每一行。
working with the reprex, I expect 30 vectors created in the working environment - for every car model for the specified column separate vector to store the value of that column for this row/carmodel and 6 more vectors that store the R commands.使用 reprex,我希望在工作环境中创建 30 个向量——对于指定列的每个汽车模型,单独的向量存储该行/汽车模型的该列的值,以及另外 6 个存储 R 命令的向量。
for example one of the vectors should look like this: cyl_MazdaRX4Wag <- 6
例如,其中一个向量应如下所示: cyl_MazdaRX4Wag <- 6
# df
df <- mtcars[1:5,]
df$carmodel <- gsub("[[:space:]]", "", rownames(df))
# create empty vectors to store R command
carmodel <- c()
mpg <- c()
cyl <- c()
hp <- c()
gear <- c()
carb <- c()
# loop through every row to create an R command
for(i in 1:nrow(df)){
carmodel[i] <- paste0("carmodel_", df$carmodel[i] , " <- ", "'", df$carmodel[i], "'",";")
mpg[i] <- paste0("mpg_", df$carmodel[i], " <- ", df$mpg[i], ";")
cyl[i] <- paste0("cyl_", df$carmodel[i], " <- ", df$cyl[i], ";")
hp[i] <- paste0("hp_", df$carmodel[i], " <- ", df$hp[i], ";")
gear[i] <- paste0("gear_", df$carmodel[i], " <- ", df$gear[i], ";")
carb[i] <- paste0("carb_", df$carmodel[i], " <- ", df$carb[i], ";")
}
# collapse the vectors in one string
carmodel <- paste(carmodel, collapse = " ")
mpg <- paste(mpg, collapse = " ")
cyl <- paste(cyl, collapse = " ")
hp <- paste(hp, collapse = " ")
gear <- paste(gear, collapse = " ")
carb <- paste(carb, collapse = " ")
# execute R command
eval(parse(text = carmodel))
eval(parse(text = mpg))
eval(parse(text = cyl))
eval(parse(text = hp))
# delete vectors that store the R commands
rm(list = c("carmodel","mpg","cyl", "hp","gear","carb"))
eval(parse(text = gear))
eval(parse(text = carb))
We can select columns on which we want to work.我们可以选择要处理的列。 Create a named vector with name and it's value.使用名称及其值创建一个命名向量。
cols <- c('carmodel', 'mpg', 'cyl', 'hp', 'gear', 'carb')
temp <- unlist(lapply(cols, function(x) as.list(setNames(df[[x]],
paste0(x, df$carmodel)))), recursive = FALSE)
Usually, it is better to keep data as a list, rather than individual objects.通常,最好将数据保存为列表,而不是单个对象。 If you need them as separate variables in the global environment we can use list2env
.如果您需要它们作为全局环境中的单独变量,我们可以使用list2env
。
list2env(temp, .GlobalEnv)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.