Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R数据帧中计算列时出现奇怪的结果?_R_If Statement_Dataframe - Fatal编程技术网

在R数据帧中计算列时出现奇怪的结果?

在R数据帧中计算列时出现奇怪的结果?,r,if-statement,dataframe,R,If Statement,Dataframe,R的新功能-在软呢帽上使用Rstudio 25。我不确定这是否是语法、数据类型或其他方面的问题,但R在其数据帧中添加/减去两列时给出了错误的输出。请看下面,让我知道问题是什么。谢谢 结果数据帧: A B C D E F G 1 true 1 0 0 0 1 2 false 1 438 32 460 2 3 false 1 100 0 100 1 4 false 1 800 136 936 5 5

R的新功能-在软呢帽上使用Rstudio 25。我不确定这是否是语法、数据类型或其他方面的问题,但R在其数据帧中添加/减去两列时给出了错误的输出。请看下面,让我知道问题是什么。谢谢

结果数据帧:

A   B       C   D   E   F   G
1   true    1   0   0   0   1
2   false   1   438 32  460 2
3   false   1   100 0   100 1
4   false   1   800 136 936 5
5   true    1   250 0   250 1
代码:


current['I']如果这是数据帧,则可能引用的列名不正确

使用


current$I我不熟悉您选择列的方式,通常您可以使用列号或名称以以下方式选择数据框中的列(对于列D):
current[,4]
current$D

然而,检查C列至G列是否为系数。如果是,问题可能在于将因子转换为数值。试一试

as.numeric(级别(当前$D))[current$D]


检查此答案:

能否粘贴
dput(头部(当前))的输出
?能否显示“错误”的输出?
C
I
G
列是什么样子的?我敢打赌,这是一个将因子转换为数字的例子,但是如果没有更多的信息,我们无法判断。
current['I'] <- as.numeric(unlist(current['D'])) + as.numeric(unlist(current['E']))
current['G'] <- as.numeric(unlist(current['F'])) - current['I']

current$C <- ifelse(current$G==0, "false", "true")

write.csv(current,"current.csv")
current$I <- as.numeric(unlist(current$D)) + as.numeric(unlist(current$E))
current$G <- as.numeric(unlist(current$F)) - current$I
current$C <- ifelse(current$G == 0, "false", "true")
write.csv(current, "current.csv")