Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following dataframe, which is the output of read_excel with missing column names in excel:

t <- tibble(A=rnorm(3), B=rnorm(3), "x"=rnorm(3), "y"=rnorm(3), Z=rnorm(3))
colnames(t)[3:4] <-  c("..3", "..4")

How can I select columns ..3 to Z in a flexible dynamic way (not depending on number or table width). I was thinking in the direction of something like:

t %>% select(-starts_with(".."):-last_col())

But this gives a warning, since starts_with returns two values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

We could force to select the first one:

t %>% select(-c(starts_with("..")[ 1 ]:last_col()))
# # A tibble: 3 x 2
#       A      B
#   <dbl>  <dbl>
# 1 0.889  0.505
# 2 0.655 -2.15 
# 3 1.34  -0.290

Or "tidier" way use first:

select(-first(starts_with("..")):-last_col())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...