I have a table like:
ID A10 A11 A12 A20 A21 A22 A30 A31 A32
Apple 1 1 1 1 1 1 1 1 1
Banana 2 2 2 2 2 2 2 2 2
Pear 3 3 3 3 3 3 3 3 3
My first question is, can I use something like "*"
to subset multiple column? Like if I want to subset columns containing A2
(A20, A21, A22), how can I do it quickly?
My second question is how can I split this dataframe to multiple tables? I would like to split this dataframe base on "A1"
"A2"
and "A3"
(related with my first question), which will be:
df1<-
ID A10 A11 A12
Apple 1 1 1
Banana 2 2 2
Pear 3 3 3
df2<-
ID A20 A21 A22
Apple 1 1 1
Banana 2 2 2
Pear 3 3 3
df3<-
ID A30 A31 A32
Apple 1 1 1
Banana 2 2 2
Pear 3 3 3
How can I do that?
For another case, I have a dataframe that the character length of the column names are different, like:
ID BSS20 BSS40 BSS60 W01 W02 W03 BW20 BW40 BW60 SS20 SS40 SS60
Apple 1 1 1 1 1 1 1 1 1
Banana 2 2 2 2 2 2 2 2 2
Pear 3 3 3 3 3 3 3 3 3
How can I split the data into three new dataframes, each one must contain W0
, like:
df1<-
ID BSS20 BSS40 BSS60 W01 W02 W03
Apple 1 1 1 1 1 1
Banana 2 2 2 2 2 2
Pear 3 3 3 3 3 3
df2<-
ID BW20 BW40 BW60 W01 W02 W03
Apple 1 1 1 1 1 1
Banana 2 2 2 2 2 2
Pear 3 3 3 3 3 3
df3<-
ID SS20 SS40 SS60 W01 W02 W03
Apple 1 1 1 1 1 1
Banana 2 2 2 2 2 2
Pear 3 3 3 3 3 3
My columns are composed of *20 *40 *60
and three special characters W01 W02 W03
, how can I express them well in r?