簡體   English   中英

select() 選擇的列比我告訴它的要多。 為什么? - R

[英]select() selects more columns than I tell it to. Why? - R

例如,當我使用dplyrselect()時:

mtcars %>% select(., cyl, disp)

它正確選擇cyldisp 但是當我在我正在研究的 dataframe 中執行此操作時,(假設它是iris ):

iris %>% select(., Sepal.Length, Sepal.Width)

即使我沒有告訴 select Petal.Length ,它也會選擇Sepal.LengthSepal.WidthPetal.Length 這非常令人沮喪,因為我在文檔、stackoverflow 或谷歌中找不到任何解釋。

最后,我想知道select()什么時候會選擇我沒有告訴它選擇的列? 有什么建議么?

編輯 - 數據:

structure(list(codigo_estacion = 11545000L, institucion = "DGA", 
    fuente = "dga_web", nombre = "Rio Baker Bajo ÑAdis", altura = 45L, 
    latitud = -47.5, longitud = -72.9749984741211, codigo_cuenca = 115L, 
    nombre_sub_cuenca = "Rio Baker Entre Arriba Rio De La Colonia Y Desemb.", 
    cantidad_observaciones = 4736L, fecha = structure(15624, class = "Date"), 
    caudal = 692, gauge_id = 11545000L, gauge_name = "Rio Baker Bajo ÑAdis", 
    precip_promedio = 0.454545468091965, temp_max_promedio = 17.0166664123535, 
    estacion_ano = "Primavera", caudal_extremo = 0, temp_extremo = 0, 
    precip_extremo = 0), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -1L), groups = structure(list(
    codigo_estacion = 11545000L, estacion_ano = "Primavera", 
    .rows = list(1L)), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

我正在使用的代碼:

df %>% dplyr::select(codigo_estacion, caudal_extremo)

但它給出了列estacion_anocodigo_estacioncaudal_extremo

您提供的數據是由變量estacion_ano分組的數據框。 在分組數據框上使用select時,分組變量將自動添加到結果中。 您可能想在使用select之前ungroup

df %>% 
  dplyr::ungroup() %>% 
  dplyr::select(codigo_estacion, caudal_extremo)

# A tibble: 1 x 2
# codigo_estacion caudal_extremo
#           <int>          <dbl>
# 1      11545000              0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM