R:使用函数参数更新数据框中的元素 [英] R: Using function arguments to update elements in a data frame

查看:63
本文介绍了R:使用函数参数更新数据框中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将数据框中引用的元素替换为我在函数中添加的参数,但是目前,它只是用最初用于定义函数的参数替换了元素(我正在寻找它难以解释-希望我的代码和图片可以澄清这一点!)

I want the elements referenced in my data frame to be replaced with the argument I put into the function, however at the moment it is just replacing the elements with the argument I used to initially define the function (I'm finding it hard to explain - hopefully my code and pictures will clarify this a bit!)

Project_assign <- function(prjct) {
  Truth_vector <- is.element((giraffe[,1]),(prjct[,1]))
  giraffe[which(Truth_vector),5] <- 'prjct'
  assign('giraffe' , giraffe , envir= .GlobalEnv)
}
Project_assign(spine_hlfs)

这通常有效,但是元素被替换为prjct而不是spine_hlfs https://i.stack.imgur.com/uuPnv.png

This mostly works however the elements get replaced with prjct instead of spine_hlfs https://i.stack.imgur.com/uuPnv.png

如果我可以按预期工作,那么接下来我将创建一个包含所有项目名称的向量,并将lapply与该功能配合使用,每几个月为我节省大量的人工工作.我对R还是比较陌生,因此可以很好地理解所有解释.

If I can get this to work as intended, then I will next create a vector with all the project names and use lapply with this function saving me a lot of manual work every few months. I am relatively new to R so any explanations are well appreciated.

推荐答案

听起来像基于(列表)查询数据框和主题数据框之间的 match ing项的简单替换之类的声音.

Sounds like a simple replace based on matching entries between a (list of) query dataframes and a subject dataframe.

这是一个基于一些模拟数据的示例.

Here is an example based on some simulated data.

我首先模拟主题 数据帧的数据:

# Sample data
giraffe <- data.frame(
    runkeys = seq(1:500),
    col1 = runif(500),
    col2 = runif(500),
    col3 = runif(500),
    col4 = runif(500));

然后我模拟2个 query dataframes runkeys 数据:

I then simulate runkeys data for 2 query dataframes:

spine_hlfs <- data.frame(
    runkeys = c(44, 260, 478));
ir_dia <- data.frame(
    runkeys = c(10, 20, 30))

查询 数据帧存储在列表中:

lst.runkeys <- list(
    spine_hlfs = spine_hlfs,
    ir_dia = ir_dia);

要标记任何查询 数据帧中存在的 runkeys 条目,我们可以使用 for 循环来每个查询 数据帧中的 match runkeys 条目:

To flag runkeys entries present in any of the query dataframes, we can use a for loop to match runkeys entries from every query dataframe:

# This is the critical line that loops through the dataframe
# and flags runkeys in giraffe with the name of the query dataframe
for (i in 1:length(lst.runkeys)) {
    giraffe[match(lst.runkeys[[i]]$runkeys, giraffe$runkeys), 5] <- names(lst.runkeys)[i];
}

这是匹配 runkeys 条目后 subject dataframe 的输出.我仅显示第5列中条目被替换的行.

This is the output of the subject dataframe after matching runkeys entries. I'm only showing rows where entries in column 5 where replaced.

giraffe[grep("(spine_hlfs|ir_dia)", giraffe[, 5]), ];
10       10 0.7401977 0.005703928 0.6778921     ir_dia
20       20 0.7954076 0.331462567 0.7637870     ir_dia
30       30 0.5772808 0.183716142 0.6984193     ir_dia
44       44 0.9701355 0.655736489 0.4917452 spine_hlfs
260     260 0.1893012 0.600140166 0.0390346 spine_hlfs
478     478 0.7655976 0.910946623 0.9779205 spine_hlfs

这篇关于R:使用函数参数更新数据框中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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