我正试图根据下面数据集中的TMtClones列来子集一个数据集。我想只为TmTClones desiree子集YldAll。然而,当我尝试子集时,结果数据不再是数字。我的数据如下。
structure(list(Year = c(2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018), Location = c("Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma", "Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma", "Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma", "Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma"), TmtClones =
c("304357.31",
"306518.1", "306519.64", "399004.19", "399078.11", "395436.8",
"397196.3", "Desiree", "304357.31", "306518.1", "306519.64",
"399004.19", "399078.11", "395436.8", "397196.3", "Desiree",
"304357.31", "306518.1", "306519.64", "399004.19", "399078.11",
"395436.8", "397196.3", "Desiree"), YldAll = c(10.3, 10.9, 8.5,
10.2, 9.5, 6.8, 4.8, 8.5, 10.9, 8.7, 9.7, 12, 10.3, 5.3, 5.7,
8.2, 10.9, 11.9, 4, 10.2, 5, 4.7, 2.5, 7.9)), row.names = c(NA,
-24L), class = c("tbl_df", "tbl", "data.frame"))
我尝试用两种不同的方式进行子集,如下面的代码所示。
YldAll.Desiree <- subset(potato.data2, TmtClones == "Desiree", select
= YldAll)
YldAll.Desiree2 <- potato.data2 %>%
filter(TmtClones == "Desiree") %>%
select(YldAll)
在这两种情况下,根据is.numeric(),产生的数据都不再是数字型的,当我试图使用它时,我得到了错误。我想不通为什么,因为is.numeric(pato.data2$YldAll)给出了TRUE。如果我试图将YldAll.Desiree或YldAll.Desiree2胁迫为数值型,我得到了错误--'list'对象不能被胁迫为'double'类型。
data.frame不能是数字,因为它是一个列表。而矩阵可以是二维数字对象。当你调用 is.numeric/as.numeric
它应该在 data.frame
而不是全部 data.frame
. 见例:
potato.data2 <- structure(
list(
Year = c(2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018),
Location = c("Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma", "Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma", "Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma", "Khangma",
"Khangma", "Khangma", "Khangma", "Khangma", "Khangma"),
TmtClones =
c("304357.31",
"306518.1", "306519.64", "399004.19", "399078.11", "395436.8",
"397196.3", "Desiree", "304357.31", "306518.1", "306519.64",
"399004.19", "399078.11", "395436.8", "397196.3", "Desiree",
"304357.31", "306518.1", "306519.64", "399004.19", "399078.11",
"395436.8", "397196.3", "Desiree"),
YldAll = c(10.3, 10.9, 8.5,
10.2, 9.5, 6.8, 4.8, 8.5, 10.9, 8.7, 9.7, 12, 10.3, 5.3, 5.7,
8.2, 10.9, 11.9, 4, 10.2, 5, 4.7, 2.5, 7.9)),
row.names = c(NA, -24L),
class = c("tbl_df", "tbl", "data.frame")
)
str(potato.data2)
#> Classes 'tbl_df', 'tbl' and 'data.frame': 24 obs. of 4 variables:
#> $ Year : num 2018 2018 2018 2018 2018 ...
#> $ Location : chr "Khangma" "Khangma" "Khangma" "Khangma" ...
#> $ TmtClones: chr "304357.31" "306518.1" "306519.64" "399004.19" ...
#> $ YldAll : num 10.3 10.9 8.5 10.2 9.5 6.8 4.8 8.5 10.9 8.7 ...
is.numeric(potato.data2)
#> [1] FALSE
is.numeric(potato.data2$YldAll)
#> [1] TRUE
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
YldAll.Desiree2 <- potato.data2 %>%
filter(TmtClones == "Desiree") %>%
select(YldAll)
str(YldAll.Desiree2)
#> tibble [3 x 1] (S3: tbl_df/tbl/data.frame)
#> $ YldAll: num [1:3] 8.5 8.2 7.9
is.numeric(YldAll.Desiree2)
#> [1] FALSE
is.numeric(YldAll.Desiree2$YldAll)
#> [1] TRUE