在 R 中进行子集化时出错 - 空数据框
Error when subsetting in R - empty data frame
我正在使用函数 subset 对数据框进行子集化,但没有成功:
这是我的数据框
> dput(df)
structure(list(Station = c("S09489500", "S09498500", "S09510200",
"S09494000", "S09497500", "S09492400", "S09504500", "S09503700"
), location = c("back", "back", "ahead", "ahead", "back", "ahead",
"ahead", "ahead"), length_years = c(36L, 75L, 33L, 34L, 75L,
35L, 49L, 34L), begin = c(1985, 1946, 1962, 1959, 1946, 1958,
1949, 1964), end = c(2020, 2020, 1994, 1992, 2020, 1992, 1997,
1997), Utest_Q7min = c(26.3618474823095, 119.756524166147, 12.749016687539,
20.3410125011518, 92.9397377831962, 19.8329511433346, 18.5949830661337,
34.4767872640756), Significance_Qtest_Q7min = c("No Independent",
"No Independent", "No Independent", "No Independent", "No Independent",
"No Independent", "No Independent", "No Independent"), PT_U = c(124,
567, 98, 181, 646, 158, 94, 238), ChangePoint_Q7min = c(26L,
19L, 9L, 17L, 29L, 23L, 30L, 16L), p_Q7min = c(0.292065629512458,
0.0219501976437697, 0.42182506012988, 0.0155276557662876, 0.00571923921900464,
0.0669830682326448, 1.28599300599023, 0.000449734648357696),
Significance_ChangePoint_Q7min = c("No significant", "Significant",
"No significant", "Significant", "Significant", "No significant",
"No significant", "Significant"), Man_Kendall = c("category 2",
"category 1", "category 1", "category 3", "category 1", "category 3",
"category 1", "category 3")), class = "data.frame", row.names = c(NA,
-8L))
我正在使用以下代码进行子集化:
df2 <- subset(df,df$Significance_ChangePoint_Q7min=="Significant" && df$Significance_Qtest_Q7min == "No Independent")
但结果我得到了一个空数据框。
有人知道为什么子集在这种情况下不起作用吗?
试试这个:
df2 <- subset(df, df$Significance_ChangePoint_Q7min == "Significant" &
df$Significance_Qtest_Q7min == "No Independent")
单人&
使用filter
library(dplyr)
df %>%
filter(Significance_ChangePoint_Q7min == "Significant" &
Significance_Qtest_Q7min == "No Independent")
这不起作用的原因是双重 &&
。在这种情况下,您需要执行一个 &
。
你想做的是比较多个值并得到一个 vector 或 TRUE
或 FALSE
值两个变量是 TRUE
或 FALSE
。单个 &
执行此操作。
双 &&
只会检查变量中的第一个值(即数据框的第一行)和 returns 单个 TRUE
或 FALSE
值而不是向量。
require(dplyr)
df2 <- df %>% filter(Significance_ChangePoint_Q7min == "Significant" & Significance_Qtest_Q7min == "No Independent")
> df2
Station location length_years begin end Utest_Q7min Significance_Qtest_Q7min PT_U ChangePoint_Q7min p_Q7min Significance_ChangePoint_Q7min Man_Kendall
1 S09498500 back 75 1946 2020 119.75652 No Independent 567 19 0.0219501976 Significant category 1
2 S09494000 ahead 34 1959 1992 20.34101 No Independent 181 17 0.0155276558 Significant category 3
3 S09497500 back 75 1946 2020 92.93974 No Independent 646 29 0.0057192392 Significant category 1
4 S09503700 ahead 34 1964 1997 34.47679 No Independent 238 16 0.0004497346 Significant category 3
我正在使用函数 subset 对数据框进行子集化,但没有成功:
这是我的数据框
> dput(df)
structure(list(Station = c("S09489500", "S09498500", "S09510200",
"S09494000", "S09497500", "S09492400", "S09504500", "S09503700"
), location = c("back", "back", "ahead", "ahead", "back", "ahead",
"ahead", "ahead"), length_years = c(36L, 75L, 33L, 34L, 75L,
35L, 49L, 34L), begin = c(1985, 1946, 1962, 1959, 1946, 1958,
1949, 1964), end = c(2020, 2020, 1994, 1992, 2020, 1992, 1997,
1997), Utest_Q7min = c(26.3618474823095, 119.756524166147, 12.749016687539,
20.3410125011518, 92.9397377831962, 19.8329511433346, 18.5949830661337,
34.4767872640756), Significance_Qtest_Q7min = c("No Independent",
"No Independent", "No Independent", "No Independent", "No Independent",
"No Independent", "No Independent", "No Independent"), PT_U = c(124,
567, 98, 181, 646, 158, 94, 238), ChangePoint_Q7min = c(26L,
19L, 9L, 17L, 29L, 23L, 30L, 16L), p_Q7min = c(0.292065629512458,
0.0219501976437697, 0.42182506012988, 0.0155276557662876, 0.00571923921900464,
0.0669830682326448, 1.28599300599023, 0.000449734648357696),
Significance_ChangePoint_Q7min = c("No significant", "Significant",
"No significant", "Significant", "Significant", "No significant",
"No significant", "Significant"), Man_Kendall = c("category 2",
"category 1", "category 1", "category 3", "category 1", "category 3",
"category 1", "category 3")), class = "data.frame", row.names = c(NA,
-8L))
我正在使用以下代码进行子集化:
df2 <- subset(df,df$Significance_ChangePoint_Q7min=="Significant" && df$Significance_Qtest_Q7min == "No Independent")
但结果我得到了一个空数据框。
有人知道为什么子集在这种情况下不起作用吗?
试试这个:
df2 <- subset(df, df$Significance_ChangePoint_Q7min == "Significant" &
df$Significance_Qtest_Q7min == "No Independent")
单人&
使用filter
library(dplyr)
df %>%
filter(Significance_ChangePoint_Q7min == "Significant" &
Significance_Qtest_Q7min == "No Independent")
这不起作用的原因是双重 &&
。在这种情况下,您需要执行一个 &
。
你想做的是比较多个值并得到一个 vector 或 TRUE
或 FALSE
值两个变量是 TRUE
或 FALSE
。单个 &
执行此操作。
双 &&
只会检查变量中的第一个值(即数据框的第一行)和 returns 单个 TRUE
或 FALSE
值而不是向量。
require(dplyr)
df2 <- df %>% filter(Significance_ChangePoint_Q7min == "Significant" & Significance_Qtest_Q7min == "No Independent")
> df2
Station location length_years begin end Utest_Q7min Significance_Qtest_Q7min PT_U ChangePoint_Q7min p_Q7min Significance_ChangePoint_Q7min Man_Kendall
1 S09498500 back 75 1946 2020 119.75652 No Independent 567 19 0.0219501976 Significant category 1
2 S09494000 ahead 34 1959 1992 20.34101 No Independent 181 17 0.0155276558 Significant category 3
3 S09497500 back 75 1946 2020 92.93974 No Independent 646 29 0.0057192392 Significant category 1
4 S09503700 ahead 34 1964 1997 34.47679 No Independent 238 16 0.0004497346 Significant category 3