小编MLn*_*wby的帖子

R 错误(从 NA 到 0):数据框中列的下标重复

我在 Stackoverflow 上查看了多个答案,但仍然无法解决我的问题。我做了一个可以工作的功能,但是我添加了一些东西,现在它不再工作了。我想将所有 NA 替换为 0,这对我来说似乎很简单。

这是我的功能,我添加了bframe[is.na(bframe)] <- 0

B <- function(frame1, frame2, column){
  bframe <- merge(frame 1, frame2, by = column, all = TRUE)
  bframe$result <- bframe$freq.x - bframe$freq.y
  bframe$percentage <- (bframe$result/bframe$freq.y)*100
  bframe[is.na(bframe)] <- 0
  return(bframe)
}

B(DT2_1, 2_1, "BurgS")
Run Code Online (Sandbox Code Playgroud)

但是,它给出了这个错误:Error in '[<-.data.frame'('* tmp *, thisvar, value = 0) : duplicate subscripts for columns

出现错误是因为存在 NA 并且无法执行计算:

BurgS  freq.x  freq.y  result   percentage  percentageABS
1      9204    184042  -174838  -94.99897   94.99897 
2      150     3034    -2884    -95.05603   95.05603 …
Run Code Online (Sandbox Code Playgroud)

r function dataframe na

5
推荐指数
1
解决办法
2万
查看次数

元音索引:Python错误"'在<string>'中需要字符串作为左操作数,而不是int"

我是初学者,关于Python,我不太了解以下内容:

我尝试打印字符串中所有元音的索引:

string = "And now for something completely different"
vowel = "aeiouAEIOU"

for i in range(0, len(string)):
    if i in vowel:  
        print(string[i])
Run Code Online (Sandbox Code Playgroud)

所以,输出应该是这样的,我想:

[0, 5, 9, 13, 15, 18, 23, 27, 29, 34, 37, 39]
Run Code Online (Sandbox Code Playgroud)

然后我收到这个错误:

"'in <string>' requires string as left operand, not int"
Run Code Online (Sandbox Code Playgroud)

我想我明白我试图比较的两个变量都应该是字符串,但是我不知道'i'是不是一个字符串,因为它确实是'string'的每一个字母.

我该如何摆脱这个错误?也许我做错了,这就是为什么这段代码无效.

我试着看一下类似的问题,但答案并不完全符合我的问题,因此我仍然没有弄明白.

python string for-loop

2
推荐指数
1
解决办法
99
查看次数

标签 统计

dataframe ×1

for-loop ×1

function ×1

na ×1

python ×1

r ×1

string ×1