[英]Select only certain columns from the data frame in R
我是R的新手。我有一個下面的數據框,需要從中選擇特定的列。
數據框如下所示:
df<-data.frame(city=as.character("Boston","Boston","Boston","Boston","Boston","Boston","Boston","Boston","Boston","Boston"),
a.Boston=c(rep(8,3),rep(6,4),9,5,7),
a.Hartford=c(rep(6,3),rep(2,4),1,5,0),
a.Denver=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_0=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_1=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_2=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_3=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_4=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_5=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_6=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_7=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_8=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_9=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_10=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_11=c(rep(8,3),rep(9,4),3,8,9),
b.Boston_12=c(rep(8,3),rep(9,4),3,8,9),
b.Denver_0=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_1=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_2=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_3=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_4=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_5=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_6=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_7=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_8=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_9=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_10=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_11=c(rep(6,3),rep(2,4),1,5,0),
b.Denver_12=c(rep(6,3),rep(2,4),1,5,0))
我想基於列的選擇創建數據幀df1到df12。 例如,df2不應包含名稱后綴為“ _1”的列,而應包含其余的列。 對於df9,不應包含具有“ _1”,“ _ 2”,...至“ _8”的列,例如b.Boston_1,b.Denver_1,b.Boston_2,b.Denver_2 ........ b.Boston_8, b.Denver_8,但包含諸如b.Boston_10,b.Denver_10,b.Boston_11,b.Denver_11之類的列。 df12僅包含諸如a.Boston,a.Hartford,a.Denver之類的列。 我想從df中刪除“城市”列。
我寫了下面的代碼:
i1 <- 0:12
for(i in seq_along(i1)) {
assign(paste0("df", i),
value = df[, setdiff(names(df),
c("city",
grep(paste("_", 0:i1[i],collapse="|", sep=""),
colnames(df), value = TRUE)))])
}
下面我得到了結果:
*df2*
a.Boston a.Hartford a.Denver b.Boston_2 b.Boston_3 b.Boston_4 b.Boston_5 b.Boston_6 b.Boston_7 b.Boston_8
1 8 6 8 8 8 8 8 8 8 8
2 8 6 8 8 8 8 8 8 8 8
b.Boston_9 b.Denver_2 b.Denver_3 b.Denver_4 b.Denver_5 b.Denver_6 b.Denver_7 b.Denver_8 b.Denver_9
1 8 6 6 6 6 6 6 6 6
2 8 6 6 6 6 6 6 6 6
df9 :
a.Boston a.Hartford a.Denver b.Boston_9 b.Denver_9
1 8 6 8 8 6
2 8 6 8 8 6
對於創建的數據幀來說,問題是,例如對於df2和df9,它們不包含從b.Denver_10到b.Denver_12和b.Boston_10到b.Boston_12的列。 從df1到df11的數據幀都沒有包含后綴_10,_11,_12的列,但應該在那兒。
所需的o / p:
df9 :
a.Boston a.Hartford a.Denver b.Boston_9 b.Boston_10 b.Boston_11 b.Boston_12 b.Denver_9 b.Denver_10
1 8 6 8 8 8 8 8 6 6
2 8 6 8 8 8 8 8 6 6
b.Denver_11 b.Denver_12
1 6 6
2 6 6
df10 :
a.Boston a.Hartford a.Denver b.Boston_10 b.Boston_11 b.Boston_12 b.Denver_10 b.Denver_11 b.Denver_12
1 8 6 8 8 8 8 6 6 6
2 8 6 8 8 8 8 6 6 6
我想要從df1到df12的這種類型的o / p。
有人可以幫我嗎?
預先感謝!!
lapply(1:12, function(k)
{
# match numbers equal or greater than k in colnames of df
pattern <- if (k < 10) paste0("\\..*\\D$|_([",k,"-9]|1[0-2])$") else
paste0("\\..*\\D$|_(1[",k%%10,"-2])$")
df[,grepl(pattern, colnames(df))]
})
編輯:在lapply
內部,我首先定義一個與所需列匹配的正則表達式模式。 即
A)包含“。”的列 並且不包含數字,后跟行尾。 (\\\\..*\\\\D$)
這與格式為a.CityName
的列匹配
B)包含“ _”后跟等於或大於給定k
且不大於12的數字的列。公式取決於k <10。 例如,對於k = 2,我們得到_([2-9]|1[0-2])$
-匹配2到9或1,然后是0到2。對於k = 11,我們得到_(1[1-2])$
。
然后,我選擇與grepl模式匹配的列。
如果我們為想要的列指定條件而不是構造正則表達式,則代碼將更加清晰。
library(dplyr)
library(stringr)
column_suffixes <- str_extract(names(df), '\\d+') %>%
as.integer
lapply(seq_len(12), function(i) {
df %>%
select_if(is.na(column_suffixes) | column_suffixes >= i) %>%
select(-city)
})
column_suffixes
只是列名稱中的一個向量整數。 如果沒有整數,則為NA
。
select_if
僅將缺少后綴或>= i
的df
列作為子集。 效果與
df[, is.na(column_suffixes) | column_suffixes >= i]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.