今天,由于数据帧子集操作,我遇到了代码中的错误.我想知道我发现的问题是一个错误还是我违反了R语义.
我正在使用R 2.15.2-61015(Trick或Treat)运行RHEL x86_64.我正在使用基础包中的子集操作.
以下代码应该是可重现的,并且它是在为此测试目的而启动的干净R控制台上运行的.
>teste <-data.frame(teste0=c(1,2,3),teste1=c(3,4,5))
>teste0<-1
>teste1<-1
>subset(teste,teste[,"teste0"]==1 & teste[,"teste1"]==1)
[1] teste0 teste1
<0 rows> (or 0-length row.names)
>subset(teste,teste[,"teste0"]==teste0 & teste[,"teste1"]==teste1)
teste0 teste1
1 1 3
2 2 4
3 3 5
Run Code Online (Sandbox Code Playgroud)
但是,如果我在子集操作之外运行逻辑代码:
>teste[,"teste0"]==teste0 & teste[,"teste1"]==teste1
[1] FALSE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)
我希望两个子集操作都会产生一个空的数据帧.但是,第二个返回完整的数据帧.这是一个错误还是我错过了R环境和名称空间的一些内容?
谢谢你的帮助,米格尔
我有以下数据框架
id val
a 1
a 2
a 3
b 4
b 5
c 6
Run Code Online (Sandbox Code Playgroud)
我想使用id的子集找到该数据帧的子集.我知道如果子集标准仅为1的值,我可以执行以下操作
y = subset(x,id=='a')
Run Code Online (Sandbox Code Playgroud)
但是,如果我有一组几个id,我如何得到一个子集.例如c('a','b').干
y = subset(x,id==c('a','b'))
Run Code Online (Sandbox Code Playgroud)
没有给我我想要的东西.
我想从数据帧上的操作中排除一个列.当然,我可以在没有要排除的列的情况下复制数据帧,但这似乎是一种解决方法.我认为必须有一种更简单的子集方式.
所以这个示例代码应该显示我的目标.
df<-data.frame(a=c(1:5),b=c(6:10),c=c(11:15))
# First subset: operate on a single column
mean(df[,1])
[1] 3
# Second subset: with a set of choosen columns
colMeans(df[,c(1,3)])
a c
3 13
# third subset: exclude column b from the operation (expected Output should be like the second subset)
colMeans(df[,!=2])
Error: unexpected '!=' in "colMeans(df[,!="
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
假设我有一个看起来像这样的data.frame:
ball1 ball2 ball3 allRed
red red blue F
red red red T
blue blue red F
. . . .
. . . .
. . . .
Run Code Online (Sandbox Code Playgroud)
等等.我希望提取allRed为TRUE的数据帧的前5行,以及allRed为FALSE的data.frame的前5行.我将data.frame存储在一个变量中ball.以下是我试图做的事情:
ball[,1][ball$allRed==F]
Run Code Online (Sandbox Code Playgroud) 我想使用dplyr复制以下内容.
set.seed(123)
my_data <- data.frame( time = c(4:13, 4:13),
var1 = rep(c("a", "b"), each=10),
var2 = rnorm(20))
my_data_new <- with(my_data,
data.frame ( time = time[var1 =="a"],
var2a = var2[var1 == "a"],
var2b = var2[var1 == "b"]))
Run Code Online (Sandbox Code Playgroud)
我尝试了与transmute()类似的语法,并获得以下错误消息
Error: wrong result size (10), expected 20 or 1
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个包含五个键的键值对,其值为每个千位对象的巨大数组.我可以在将它传递给函数时将其子集化,或者我可以在函数中传递整个数组.
例如:
keyVal ={"first_array": Object[1000],
"second_array": Object[1000],
"third_array": Object[1000],
"fourth_array": Object[1000]
}
var first_arr = keyVal.first_array
Run Code Online (Sandbox Code Playgroud)
两种可能性:
function notSubsettedArgs(keyVal){
$.each(keyVal.first_array,function(i,item){
//some processing});
}
function subsettedArgs(first_array){
$.each(first_array,function(i,item){
//do some processing});
}
Run Code Online (Sandbox Code Playgroud)
它在javascript中有所不同吗?
函数是否承担参数大小的负载?
谢谢
这很奇怪.当我尝试选择我的列data.table做
df1[, 30]
Run Code Online (Sandbox Code Playgroud)
它只给了我30,或者我放在那里的任何数字.不是第30栏.
数据:https://github.com/pourque/country-data/blob/master/data/df1.csv
我已经检查了,当我刚刚进行测试时,一切正常data.frame:
df2 <- data.frame(x = 1:3, y = 3:1, z = 7:9)
> df2[, 2]
[1] 3 2 1
Run Code Online (Sandbox Code Playgroud)
关于可能发生的事情的任何想法?
我已经对我的df进行了攻击,为渔业区域和不同的渔具和物种制作了一系列着陆(重量)时间序列.我想删除每个捕鱼区域的所有行+渔具+ Species.Code组合,其中时间序列的平均着陆重量小于10吨.
这是我的代码的一个例子(每个组合的年份范围并不总是相同);
Year Species.Code gear region Landings.t
1988 COD creel Greece 1
1992 COD creel Greece 2
1994 COD creel Greece 1
1996 COD creel Greece 2
2001 COD creel Greece 1
2002 COD creel Greece 1
2003 COD creel Greece 1
1984 LOB creel Cyprus 24
1985 LOB creel Cyprus 18
1986 LOB creel Cyprus 21
1987 LOB creel Cyprus 10
1988 LOB creel Cyprus 38
1989 LOB creel Cyprus 35
1990 LOB creel Cyprus 29
1991 LOB …Run Code Online (Sandbox Code Playgroud) 我是循环的新手.我有一个笨重的数据框架,我想减少,以便只保留没有负数的观察(行).这是我被困的地方.这会每次创建一个空值而不是精简数据帧.
mydata=for (i in names(df)) {
subset(df, df[[ paste(i)]]>=0)
}
Run Code Online (Sandbox Code Playgroud) 我希望这不是一个统计问题......
假设我有一个界面:
public interface PairValidatable<T>
{
public boolean isValidWith(T);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我有一个大型的PairValidatables数组,如何找到每个对通过isValidWith测试的那个数组的最大子集?
为了澄清,如果子集中有三个条目,则元素0和1应传递isValidWith,元素1和2应传递isValidWith,元素0和2应传递isValidWith.
例,
public class Point implements PairValidatable<Point>
{
int x;
int y;
public Point(int xIn, int yIn)
{
x = xIn;
y = yIn;
}
public boolean isValidWith(Point other)
{
//whichever has the greater x must have the lesser (or equal) y
return x > other.x != y > other.y;
}
}
Run Code Online (Sandbox Code Playgroud)
直观的想法是保持一个点向量,添加数组元素0,并将每个剩余的数组元素与向量进行比较,如果它通过向量中的每个元素进行验证,如果是这样就将它添加到向量...但是问题元素0可能是非常严格的.例如,
Point[] arr = new Point[5];
arr[0] = new Point(1000, 1000);
arr[1] = new Point(10, 10); …Run Code Online (Sandbox Code Playgroud)