gpt4 book ai didi

r - 如何迭代地将列传递给R中的函数

转载 作者:行者123 更新时间:2023-12-04 00:12:54 24 4
gpt4 key购买 nike

我对 R 和堆栈溢出不熟悉,而且我在编码方面没有经验,我希望能得到一些帮助。我有一个数据框,我想对多个变量执行相同的操作。我为要执行的操作编写了一个函数,但我不确定如何更改列名,以便该函数分别作用于每个变量。

#Fake Data

#index for a list of traits, and the current food type for each pet

shelterpets <- base::data.frame(
ID = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"),
index_agility = round(runif(10, min=-0.4, max=0.4), digits = 2),
index_boldness = round(runif(10, min=-0.4, max=0.4), digits = 2),
index_curiousity = round(runif(10, min=-0.4, max=0.4), digits = 2),
index_dexterity = round(runif(10, min=-0.4, max=0.4), digits = 2),
index_empathy = round(runif(10, min=-0.4, max=0.4), digits = 2),
food_type = c("diet_food", "diet_food", "regular_food", "diet_food", "regular_food", "regular_food", "regular_food", "diet_food", "diet_food", "regular_food")
)



# function to look at index for each trait, current food type, and suggest changes to food type
function(petfood) {

# variable to capture predicted food type: diet_food, regular_food
shelterpets$food10_trait <- NA


#pet previously on diet_food and above 0.10 then confirm diet_food, else predict regular_food
shelterpets$food10_trait <- ifelse(shelterpets$food_type == "diet_food",
ifelse(shelterpets$index_trait >= 0.10, "diet_food", "regular_food"),
shelterpets$food10_trait)


#pet previously on regular_food and below -0.10 then confirm regular_food, else predict diet_food
shelterpets$food10_trait <- ifelse(shelterpets$food_type == "regular_food",
ifelse(shelterpets$index_trait <= -0.10, "regular_food", "diet_food" ),
shelterpets$food10_trait)


#typecast
shelterpets$food10_trait <- as.factor(shelterpets$food10_trait)


#update trait so replace "trait" with "agility", then "boldness", etc.
}

我希望它看起来像

 ID index_agility index_boldness index_curiousity index_dexterity index_empathy    food_type food10_agility food10_boldness
1 1 0.26 -0.28 0.17 0.17 0.28 diet_food diet_food regular_food
2 2 0.17 -0.12 -0.25 0.06 0.06 diet_food diet_food regular_food
3 3 0.24 0.14 -0.13 0.25 0.28 regular_food diet_food diet_food
4 4 -0.07 0.30 -0.32 0.06 0.23 diet_food regular_food diet_food
5 5 0.33 0.00 0.13 0.23 -0.18 regular_food diet_food diet_food
6 6 0.17 -0.20 0.01 0.25 0.17 regular_food diet_food regular_food

food10_curiousity food10_dexterity food10_empathy
1 diet_food diet_food diet_food
2 regular_food regular_food regular_food
3 regular_food diet_food diet_food
4 regular_food regular_food diet_food
5 diet_food diet_food regular_food
6 diet_food diet_food diet_food

我做这个是为了开始


#get names in array to hopefully pass to the function, so drop ID and food_type
pet <- as.matrix(colnames(shelterpets))
pet <- pet[-c(1,7),,drop=F]

我看到了这些问题,但我并没有完全理解它们的工作方式以适应它们:

  1. Pass a data.frame column name to a function
  2. How to iteratively pass arguments into an R function

感谢您的指点。

最佳答案

您的尝试即将完成,但您可以遍历每个特征并使用函数的结果为您的数据框分配一个新列。我做了一些小改动:

ifelse(shelterpets$index_trait

ifelse(shelterpets[, paste0('index_', trait)]

输入可以是每个trait作为字符串,返回值可以是as.factor(...)

# function to look at index for each trait, current food type, and suggest changes to food type
f <- function(trait, data = shelterpets) {

# variable to capture predicted food type: diet_food, regular_food
data$food10_trait <- NA


#pet previously on diet_food and above 0.10 then confirm diet_food, else predict regular_food
data$food10_trait <- ifelse(data$food_type == "diet_food",
ifelse(data[, paste0('index_', trait)] >= 0.10, "diet_food", "regular_food"),
data$food10_trait)


#pet previously on regular_food and below -0.10 then confirm regular_food, else predict diet_food
data$food10_trait <- ifelse(data$food_type == "regular_food",
ifelse(data[, paste0('index_', trait)] <= -0.10, "regular_food", "diet_food" ),
data$food10_trait)


#typecast
as.factor(data$food10_trait)


#update trait so replace "trait" with "agility", then "boldness", etc.
}

## test
f('agility')
# [1] diet_food diet_food regular_food regular_food diet_food regular_food regular_food diet_food regular_food diet_food
# Levels: diet_food regular_food

应用于每个特征

traits <- gsub('.*_', '', grep('index', names(shelterpets), value = TRUE))
shelterpets[, paste0('food10_', traits)] <- lapply(traits, f)

# ID index_agility index_boldness index_curiousity index_dexterity index_empathy food_type food10_agility food10_boldness food10_curiousity food10_dexterity food10_empathy
# 1 1 0.06 -0.34 -0.25 0.28 0.22 diet_food regular_food regular_food regular_food diet_food diet_food
# 2 2 0.37 -0.01 -0.13 0.22 0.35 diet_food diet_food regular_food regular_food diet_food diet_food
# 3 3 0.33 -0.07 -0.03 0.20 0.22 regular_food diet_food diet_food diet_food diet_food diet_food
# 4 4 0.07 -0.23 -0.14 -0.29 0.05 diet_food regular_food regular_food regular_food regular_food regular_food
# 5 5 0.23 0.06 0.09 0.24 -0.17 regular_food diet_food diet_food diet_food diet_food regular_food
# 6 6 -0.27 -0.19 -0.23 0.37 -0.35 regular_food regular_food regular_food regular_food diet_food regular_food
# 7 7 0.17 0.30 -0.14 -0.14 -0.11 regular_food diet_food diet_food regular_food regular_food regular_food
# 8 8 -0.22 0.13 0.21 -0.06 0.08 diet_food regular_food diet_food diet_food regular_food regular_food
# 9 9 -0.25 0.21 -0.02 0.09 -0.29 diet_food regular_food diet_food regular_food regular_food regular_food
# 10 10 -0.35 0.39 -0.34 0.20 0.13 regular_food regular_food diet_food regular_food diet_food diet_food

关于r - 如何迭代地将列传递给R中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67082872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com