- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们在数据框 df1 和一列中确实有很多数据列(名称为 mycols 以及一些在本例中不应处理的未命名列) subj 也是另一个数据框 df2 的索引,其中包含 repl 和 subj 列(在第二个数据框中是 < strong>subj unique)和许多其他不重要的列(它们在此的唯一作用是,我们不能假设只有 2 列)。
我想以这样的方式替换列的子集( df1[,mycols] ),即如果存在 NA ( df1[ ,mycols][is.na(df1[,mycols])] ) <- 替换为 df2$repl 列的值,其中 df2 中的行有df2$subj = df1$subj。
编辑:示例数据(我不知道将其写入数据帧分配的命令):
mycols = c("a","b")
df1:
subj a b c
1 NA NA 1
1 2 3 5
2 0 NA 2
3 8 8 8
df2:
subj repl notinterested
1 5 1000
2 6 0
3 40 10
result:
df1-transformed-to:
subj a b c
1 5 5 1 #the 2 fives appeared by lookup
1 2 3 5
2 0 6 2 #the 6 appeared
3 8 8 8
我想出了以下代码:
df1[,mycols][is.na(df1[,mycols])] <- df2[match( df1$subj, df2$subj),"repl"]
但问题是(我认为),右侧的大小与左侧的大小不同 - 我认为它可能适用于“mycols”中的一列,但我想对所有mycols执行相同的操作(如果NA,则查找表df2并替换 - 替换值在范围内相同行)。
(此外,我需要每次都按名称 mycols 明确枚举列,因为可能还有其他列)
作为关于编程风格的小问题 - 在 R 中,编写此操作的良好且快速的方法是什么?如果它是一种过程语言,我们可以进行改造
df1[,mycols][is.na(df1[,mycols])]
采用一种我认为更好、更具可读性的方法:
function(x){ *x[is.na(*x)] }
function(& df1[,mycols])
并确保没有任何内容被不必要地从一个地方复制到另一个地方。
最佳答案
使用您的代码,我们需要复制
“repl”列以使两个子集数据集相等,然后像您一样分配值
val <- df2$repl[match(df1$subj, df2$subj)][row(df1[mycols])][is.na(df1[mycols])]
df1[mycols][is.na(df1[mycols])] <- val
df1
# subj a b c
#1 1 5 5 1
#2 1 2 3 5
#3 2 0 6 2
#4 3 8 8 8
使用data.table
的另一个选项
library(data.table)#v1.9.5+
DT <- setDT(df1, key='subj')[df2[c('subj', 'repl')]]
for(j in mycols){
i1 <- which(is.na(DT[[j]]))
set(DT, i=i1, j=j, value= DT[['repl']][i1])
}
DT[,repl:= NULL]
# subj a b c
#1: 1 5 5 1
#2: 1 2 3 5
#3: 2 0 6 2
#4: 3 8 8 8
或者使用dplyr
library(dplyr)
left_join(df1, df2, by='subj') %>%
mutate_each_(funs(ifelse(is.na(.),repl,.)), mycols) %>%
select(a:c)
# a b c
#1 5 5 1
#2 2 3 5
#3 0 6 2
#4 8 8 8
df1 <- structure(list(subj = c(1L, 1L, 2L, 3L), a = c(NA, 2L, 0L, 8L
), b = c(NA, 3L, NA, 8L), c = c(1L, 5L, 2L, 8L)), .Names = c("subj",
"a", "b", "c"), class = "data.frame", row.names = c(NA, -4L))
df2 <- structure(list(subj = 1:3, repl = c(5L, 6L, 40L),
notinterested = c(1000L,
0L, 10L)), .Names = c("subj", "repl", "notinterested"),
class = "data.frame", row.names = c(NA, -3L))
关于R 有条件地通过查找替换更多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045512/
我想对一个字符串执行搜索和替换,比如 password。 正如您从问题中了解到的那样,替换后的字符串应变为 sdvvzrug。 但不幸的是,下面的代码输出bbbbcaab: $search = ran
我正在使用 futurize --stage2它应用了许多源代码转换以使代码 python2 和 python3 兼容。其中一个修复是所有分区 a/b 都替换为 old_div(a/b),我想避免这种
我正在使用 RStudio,但我在控制台上的输出被截断了。我找不到如何停止截断(我尝试搜索 ?options 以及在谷歌上搜索的时间比我想承认的要长)。 编辑:我向大家道歉!我最初的长名称为“This
我有一个 fragment 堆栈,我在其中使用替换和相加。添加或替换我的 fragment 的代码(在我的 Activity 中)如下 private fun addFragment(fragment
我在一个数组中插入了一些字符串,但在我这样做之前,我想按照主题所说的去做。只用 %20 替换空格,我这样做: Name.push(linkText.replace(" ", "%20")); 但是我如
我正在尝试编译和测试我在网上看到的代码 Expanding an IP add 。但是,当我尝试编译它时,我收到有关 StringBuilder 替换方法的错误。它说: IPadd.java:52:
我正在尝试使用 dplyr 的最新功能重写我的部分代码,方法是将 data.frame() 替换为 data_frame() 和 cbind() 与 bind_cols(): library(rgeo
我最近偶然发现了 replace()和 "[ x.tst s.tst s.tst [,1] [,2] [,3] [1,] 0 0 0
我一直想知道,如何在给定的参数内进行替换。 如果你有这样的一行: 123,Hello,World,(I am, here), unknown 你想更换 World与 Foobar那么这是一个简单的任务
如何转义字符串中的双引号?例如, input: "Nobody" output: \"Nobody\" 我尝试过这样的操作,但不起作用: String name = "Nobody"; name.r
我正在做类似的事情: SQL sql sQl SqL var ps = document.getElementsByTagName('p'); for(var i = 0; i 但它不会替换文本。
我正在尝试用 \" 替换所有 " 并用 JSON 解析字符串,但浏览器抛出错误 SyntaxError: JSON Parse error: Unrecognized token '\'. 下面是代码
大家好,在这里挣扎...... 是否可以将第一个正斜杠之间的任何内容替换为“”,但保留其余部分? 例如var 将是 string "/anything-here-this-needs-to-be-re
在下面的代码中,JavaScript 替换函数中的 alert(a) 将提醒匹配的字符串,在本例中,将是 {name} 和 {place}。 这按照文档 javascript docs 的描述工作,即
+-----------------------------+ | tables | +-------------------
我正在尝试用\"替换包含 "的字符串,下面是我尝试过的程序 String s="\"/test /string\""; s = s.replaceAll("\"", "\\\"");
var text = "a's ..a's ...\"... "; text = convert(text); function convert( text ) { var n = text
我正在尝试使用 JavaScript 中的替换函数,但有一个问题。 strNewDdlVolCannRegion = strNewDdlVolCannRegion.replace(/_existing
好吧,首先我对我的上一篇文章感到非常抱歉,但我真的需要帮助,我会把我真正想要的东西放在一个更清晰的代码中。我不擅长 javascript,所以希望你能帮助我。
我正在写一张纸条,遇到了障碍。可能有更有效的方法来执行此操作,但我对 Python 还很陌生。我正在尝试创建用户生成的 IP 地址列表。我正在使用 print 来查看生成的值是否正确。当我运行此代码时
我是一名优秀的程序员,十分优秀!