R 中 `subset` 函数的逻辑参数到底是什么?
What exactly does the logical parameter on the `subset` function in R?
我正在学习 R 书学习 R - Richard Cotton,第 5 章:列表和数据框,我不理解这个例子,我有这个数据框和以下脚本:
(a_data_frame <- data.frame(
x = letters[1:5],
y = rnorm(5),
z = runif(5) > 0.5
))
x y z
1 a 0.6395739 FALSE
2 b -1.1645383 FALSE
3 c -1.3616093 FALSE
4 d 0.5658254 FALSE
5 e 0.4345538 FALSE
subset(a_data_frame, y > 0 | z, x) # what exactly mean y > 0 | z ?
看了书说:
subset takes up to three arguments: a data frame to subset, a
logical vector of conditions for rows to include, and a vector of
column names to keep
没有关于第二个逻辑参数的更多信息。
这是一个棘手的例子,因为 (a_data_frame, y > 0 | z, x)
第二个参数表示 y > 0,而“| z”表示 或 z 列中的值为 True。
y>0 评估 rnorm(5) 给出的值你的值与书上的不同,因为它们也是随机生成的 "or" "|"如果条件为 True,则在选择列 z 的情况下使用符号,在您的情况下,所有值都为 False,您看不到发生了什么,但作为教学示例,如果我们改为 z = rnorm(5)
runif(5)>5
,您可以更好地理解此功能的工作原理。
(a_data_frame <- data.frame(
x = letters[1:5],
y = rnorm(5),
z = rnorm(5)
))
x y z
1 a -0.91016367 2.04917552
2 b 0.01591093 0.03070526
3 c 0.19146220 -0.42056236
4 d 1.07171934 1.31511485
5 e 1.14760483 -0.09855757
So If we have y<0 or z<0 the output of column will be the row a,c,e
> subset(a_data_frame, y < 0 | z < 0, x)
x
1 a
3 c
5 e
> subset(a_data_frame, y < 0 & z<0, x)
[1] x
<0 rows> (or 0-length row.names) # there is no values for y<0 and z<0
> subset(a_data_frame, y < 0 & z, x) # True for row 2.
x
2 b
> subset(a_data_frame, y < 0 | z, x) # true for row 2 and row 4.
x
2 b
4 d
我正在学习 R 书学习 R - Richard Cotton,第 5 章:列表和数据框,我不理解这个例子,我有这个数据框和以下脚本:
(a_data_frame <- data.frame(
x = letters[1:5],
y = rnorm(5),
z = runif(5) > 0.5
))
x y z
1 a 0.6395739 FALSE
2 b -1.1645383 FALSE
3 c -1.3616093 FALSE
4 d 0.5658254 FALSE
5 e 0.4345538 FALSE
subset(a_data_frame, y > 0 | z, x) # what exactly mean y > 0 | z ?
看了书说:
subset takes up to three arguments: a data frame to subset, a logical vector of conditions for rows to include, and a vector of column names to keep
没有关于第二个逻辑参数的更多信息。
这是一个棘手的例子,因为 (a_data_frame, y > 0 | z, x)
第二个参数表示 y > 0,而“| z”表示 或 z 列中的值为 True。
y>0 评估 rnorm(5) 给出的值你的值与书上的不同,因为它们也是随机生成的 "or" "|"如果条件为 True,则在选择列 z 的情况下使用符号,在您的情况下,所有值都为 False,您看不到发生了什么,但作为教学示例,如果我们改为 z = rnorm(5)
runif(5)>5
,您可以更好地理解此功能的工作原理。
(a_data_frame <- data.frame(
x = letters[1:5],
y = rnorm(5),
z = rnorm(5)
))
x y z
1 a -0.91016367 2.04917552
2 b 0.01591093 0.03070526
3 c 0.19146220 -0.42056236
4 d 1.07171934 1.31511485
5 e 1.14760483 -0.09855757
So If we have y<0 or z<0 the output of column will be the row a,c,e
> subset(a_data_frame, y < 0 | z < 0, x)
x
1 a
3 c
5 e
> subset(a_data_frame, y < 0 & z<0, x)
[1] x
<0 rows> (or 0-length row.names) # there is no values for y<0 and z<0
> subset(a_data_frame, y < 0 & z, x) # True for row 2.
x
2 b
> subset(a_data_frame, y < 0 | z, x) # true for row 2 and row 4.
x
2 b
4 d