How to separate a dataframe based on specific string in column name [duplicate]Split string by last two characters in R? (/negative string indices)How to sort a dataframe by multiple column(s)Drop data frame columns by nameSplit column at delimiter in data frameSplit comma-separated strings in a column into separate rowsDynamically select data frame columns using $ and a vector of column nameshow to make a bar plot for a list of dataframes?Divide multiple columns of one data frame by row names value of another dataframe RR dataframe combining rows based on columnsSearching for a list of string in a dataframe in Rconvert 2 columns dataframe to matrix by slicing rownames - R
How can I support myself financially as a 17 year old with a loan?
Independent, post-Brexit Scotland - would there be a hard border with England?
How to model the curly cable part of the phone
I drew a randomly colored grid of points with tikz, how do I force it to remember the first grid from then on?
Have I damaged my car by attempting to reverse with hand/park brake up?
Out of scope work duties and resignation
Lie super algebra presentation of the Kähler identities
Does a card have a keyword if it has the same effect as said keyword?
How to apply differences on part of a list and keep the rest
Why does this rising edge detector using a capacitor and a resistor work?
Which module had more 'comfort' in terms of living space, the Lunar Module or the Command module?
Understanding trademark infringements in a world where many dictionary words are trademarks?
Is latino sine flexione dead?
Why do only some White Walkers shatter into ice chips?
How might a mountain bowl form?
Manager is threatening to grade me poorly if I don't complete the project
Are there any Final Fantasy Spirits in Super Smash Bros Ultimate?
What is the most remote airport from the center of the city it supposedly serves?
Using field size much larger than necessary
Upside-Down Pyramid Addition...REVERSED!
BOOM! Perfect Clear for Mr. T
Make some Prime Squares!
Why was the battle set up *outside* Winterfell?
What to use instead of cling film to wrap pastry
How to separate a dataframe based on specific string in column name [duplicate]
Split string by last two characters in R? (/negative string indices)How to sort a dataframe by multiple column(s)Drop data frame columns by nameSplit column at delimiter in data frameSplit comma-separated strings in a column into separate rowsDynamically select data frame columns using $ and a vector of column nameshow to make a bar plot for a list of dataframes?Divide multiple columns of one data frame by row names value of another dataframe RR dataframe combining rows based on columnsSearching for a list of string in a dataframe in Rconvert 2 columns dataframe to matrix by slicing rownames - R
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Split string by last two characters in R? (/negative string indices)
5 answers
I have a huge data that I cannot split into two sets
df<- structure(list(name = structure(1:3, .Label = c("a", "b", "c"
), class = "factor"), X3C_AALI_01A = c(651L, 2L, 1877L), X3C_AALJ_01B = c(419L,
2L, 1825L), X3C_AALK_01A = c(1310L, 52L, 1286L), X4H_AAAK_11B = c(2978L,
4L, 1389L), X5L_AAT0_01B = c(2576L, 15L, 1441L), X5L_AAT1_01A = c(2886L,
5L, 921L), X5T_A9QA_03A = c(929L, 3L, 935L), A1_A0SI_10A = c(1578L,
1L, 2217L), A1_A0SK_07C = c(3003L, 6L, 2984L), A1_A0SO_01A = c(6413L,
0L, 3577L), A1_A0SP_05B = c(5157L, 5L, 4596L), A2_A04P_01A = c(4283L,
6L, 2508L), X5L_AAh1_10A = c(2886L, 5L, 921L), X5T_A0QA_03A = c(929L,
3L, 935L), A1_A0Sm_10A = c(1578L, 1L, 2217L), A1_ArSK_01A = c(3003L,
6L, 2984L), A1_AfSO_01A = c(6413L, 0L, 3577L), A1_AuSP_05A = c(5157L,
5L, 4596L), A2_Ap4P_11A = c(4283L, 6L, 2508L)), class = "data.frame", row.names = c(NA,
-3L))
basically , I want to split the data based on the last character of the column name. for example if you look at the above data, the second column is like this 3C_AALI_01A which I want to generate two data sets based on the _01A
So those columns that have 01 to 09 values I want them to be in one data frame and those ones that have 10 to whatever number want them to be in the second data frame. For example in the above example data.
the columns with the following names should be in one data frame
3C_AALI_01A
3C_AALJ_01B
3C_AALK_01A
5L_AAT0_01B
5L_AAT1_01A
5T_A9QA_03A
A1_A0SK_07C
A1_A0SO_01A
A1_A0SP_05B
A2_A04P_01A
5T_A0QA_03A
A1_ArSK_01A
A1_AfSO_01A
A1_AuSP_05A
and the columns with the following names should be in another data frame
4H_AAAK_11B
A1_A0SI_10A
5L_AAh1_10A
A1_A0Sm_10A
A2_Ap4P_11A
r
marked as duplicate by smci, Maurits Evers
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Apr 12 at 5:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Split string by last two characters in R? (/negative string indices)
5 answers
I have a huge data that I cannot split into two sets
df<- structure(list(name = structure(1:3, .Label = c("a", "b", "c"
), class = "factor"), X3C_AALI_01A = c(651L, 2L, 1877L), X3C_AALJ_01B = c(419L,
2L, 1825L), X3C_AALK_01A = c(1310L, 52L, 1286L), X4H_AAAK_11B = c(2978L,
4L, 1389L), X5L_AAT0_01B = c(2576L, 15L, 1441L), X5L_AAT1_01A = c(2886L,
5L, 921L), X5T_A9QA_03A = c(929L, 3L, 935L), A1_A0SI_10A = c(1578L,
1L, 2217L), A1_A0SK_07C = c(3003L, 6L, 2984L), A1_A0SO_01A = c(6413L,
0L, 3577L), A1_A0SP_05B = c(5157L, 5L, 4596L), A2_A04P_01A = c(4283L,
6L, 2508L), X5L_AAh1_10A = c(2886L, 5L, 921L), X5T_A0QA_03A = c(929L,
3L, 935L), A1_A0Sm_10A = c(1578L, 1L, 2217L), A1_ArSK_01A = c(3003L,
6L, 2984L), A1_AfSO_01A = c(6413L, 0L, 3577L), A1_AuSP_05A = c(5157L,
5L, 4596L), A2_Ap4P_11A = c(4283L, 6L, 2508L)), class = "data.frame", row.names = c(NA,
-3L))
basically , I want to split the data based on the last character of the column name. for example if you look at the above data, the second column is like this 3C_AALI_01A which I want to generate two data sets based on the _01A
So those columns that have 01 to 09 values I want them to be in one data frame and those ones that have 10 to whatever number want them to be in the second data frame. For example in the above example data.
the columns with the following names should be in one data frame
3C_AALI_01A
3C_AALJ_01B
3C_AALK_01A
5L_AAT0_01B
5L_AAT1_01A
5T_A9QA_03A
A1_A0SK_07C
A1_A0SO_01A
A1_A0SP_05B
A2_A04P_01A
5T_A0QA_03A
A1_ArSK_01A
A1_AfSO_01A
A1_AuSP_05A
and the columns with the following names should be in another data frame
4H_AAAK_11B
A1_A0SI_10A
5L_AAh1_10A
A1_A0Sm_10A
A2_Ap4P_11A
r
marked as duplicate by smci, Maurits Evers
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Apr 12 at 5:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Really you want negative string indexing. Also, your dataframe is kind of transposed, it would be more normal to have one single column name with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string. If your dataframe was structured like that, you could usetidyr::separate(..., sep=-1)
– smci
Apr 10 at 10:47
add a comment |
This question already has an answer here:
Split string by last two characters in R? (/negative string indices)
5 answers
I have a huge data that I cannot split into two sets
df<- structure(list(name = structure(1:3, .Label = c("a", "b", "c"
), class = "factor"), X3C_AALI_01A = c(651L, 2L, 1877L), X3C_AALJ_01B = c(419L,
2L, 1825L), X3C_AALK_01A = c(1310L, 52L, 1286L), X4H_AAAK_11B = c(2978L,
4L, 1389L), X5L_AAT0_01B = c(2576L, 15L, 1441L), X5L_AAT1_01A = c(2886L,
5L, 921L), X5T_A9QA_03A = c(929L, 3L, 935L), A1_A0SI_10A = c(1578L,
1L, 2217L), A1_A0SK_07C = c(3003L, 6L, 2984L), A1_A0SO_01A = c(6413L,
0L, 3577L), A1_A0SP_05B = c(5157L, 5L, 4596L), A2_A04P_01A = c(4283L,
6L, 2508L), X5L_AAh1_10A = c(2886L, 5L, 921L), X5T_A0QA_03A = c(929L,
3L, 935L), A1_A0Sm_10A = c(1578L, 1L, 2217L), A1_ArSK_01A = c(3003L,
6L, 2984L), A1_AfSO_01A = c(6413L, 0L, 3577L), A1_AuSP_05A = c(5157L,
5L, 4596L), A2_Ap4P_11A = c(4283L, 6L, 2508L)), class = "data.frame", row.names = c(NA,
-3L))
basically , I want to split the data based on the last character of the column name. for example if you look at the above data, the second column is like this 3C_AALI_01A which I want to generate two data sets based on the _01A
So those columns that have 01 to 09 values I want them to be in one data frame and those ones that have 10 to whatever number want them to be in the second data frame. For example in the above example data.
the columns with the following names should be in one data frame
3C_AALI_01A
3C_AALJ_01B
3C_AALK_01A
5L_AAT0_01B
5L_AAT1_01A
5T_A9QA_03A
A1_A0SK_07C
A1_A0SO_01A
A1_A0SP_05B
A2_A04P_01A
5T_A0QA_03A
A1_ArSK_01A
A1_AfSO_01A
A1_AuSP_05A
and the columns with the following names should be in another data frame
4H_AAAK_11B
A1_A0SI_10A
5L_AAh1_10A
A1_A0Sm_10A
A2_Ap4P_11A
r
This question already has an answer here:
Split string by last two characters in R? (/negative string indices)
5 answers
I have a huge data that I cannot split into two sets
df<- structure(list(name = structure(1:3, .Label = c("a", "b", "c"
), class = "factor"), X3C_AALI_01A = c(651L, 2L, 1877L), X3C_AALJ_01B = c(419L,
2L, 1825L), X3C_AALK_01A = c(1310L, 52L, 1286L), X4H_AAAK_11B = c(2978L,
4L, 1389L), X5L_AAT0_01B = c(2576L, 15L, 1441L), X5L_AAT1_01A = c(2886L,
5L, 921L), X5T_A9QA_03A = c(929L, 3L, 935L), A1_A0SI_10A = c(1578L,
1L, 2217L), A1_A0SK_07C = c(3003L, 6L, 2984L), A1_A0SO_01A = c(6413L,
0L, 3577L), A1_A0SP_05B = c(5157L, 5L, 4596L), A2_A04P_01A = c(4283L,
6L, 2508L), X5L_AAh1_10A = c(2886L, 5L, 921L), X5T_A0QA_03A = c(929L,
3L, 935L), A1_A0Sm_10A = c(1578L, 1L, 2217L), A1_ArSK_01A = c(3003L,
6L, 2984L), A1_AfSO_01A = c(6413L, 0L, 3577L), A1_AuSP_05A = c(5157L,
5L, 4596L), A2_Ap4P_11A = c(4283L, 6L, 2508L)), class = "data.frame", row.names = c(NA,
-3L))
basically , I want to split the data based on the last character of the column name. for example if you look at the above data, the second column is like this 3C_AALI_01A which I want to generate two data sets based on the _01A
So those columns that have 01 to 09 values I want them to be in one data frame and those ones that have 10 to whatever number want them to be in the second data frame. For example in the above example data.
the columns with the following names should be in one data frame
3C_AALI_01A
3C_AALJ_01B
3C_AALK_01A
5L_AAT0_01B
5L_AAT1_01A
5T_A9QA_03A
A1_A0SK_07C
A1_A0SO_01A
A1_A0SP_05B
A2_A04P_01A
5T_A0QA_03A
A1_ArSK_01A
A1_AfSO_01A
A1_AuSP_05A
and the columns with the following names should be in another data frame
4H_AAAK_11B
A1_A0SI_10A
5L_AAh1_10A
A1_A0Sm_10A
A2_Ap4P_11A
This question already has an answer here:
Split string by last two characters in R? (/negative string indices)
5 answers
r
r
edited Apr 10 at 10:45
smci
15.8k679110
15.8k679110
asked Mar 22 at 22:13
LearnerLearner
38010
38010
marked as duplicate by smci, Maurits Evers
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Apr 12 at 5:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by smci, Maurits Evers
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Apr 12 at 5:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Really you want negative string indexing. Also, your dataframe is kind of transposed, it would be more normal to have one single column name with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string. If your dataframe was structured like that, you could usetidyr::separate(..., sep=-1)
– smci
Apr 10 at 10:47
add a comment |
Really you want negative string indexing. Also, your dataframe is kind of transposed, it would be more normal to have one single column name with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string. If your dataframe was structured like that, you could usetidyr::separate(..., sep=-1)
– smci
Apr 10 at 10:47
Really you want negative string indexing. Also, your dataframe is kind of transposed, it would be more normal to have one single column name with the names, and numerical columns
a, b, c
. Like t(df)
without the unwanted coercion to string. If your dataframe was structured like that, you could use tidyr::separate(..., sep=-1)
– smci
Apr 10 at 10:47
Really you want negative string indexing. Also, your dataframe is kind of transposed, it would be more normal to have one single column name with the names, and numerical columns
a, b, c
. Like t(df)
without the unwanted coercion to string. If your dataframe was structured like that, you could use tidyr::separate(..., sep=-1)
– smci
Apr 10 at 10:47
add a comment |
2 Answers
2
active
oldest
votes
df1 <- df[,grep('0[1-9].$',colnames(df))]
df2 <- df[,-grep('0[1-9].$',colnames(df))]
Quotes in at least two of your answers are incorrect. Please edit.
– Roman Luštrik
Mar 22 at 22:30
Could you elaborate? Or edit and I will accept?
– Wil
Mar 22 at 22:38
1
It was an issue with the keyboard on this device. Do they appear correct to you now?
– Wil
Mar 22 at 22:49
add a comment |
You could use tidyr::separate(..., last=-1)
approach
- which uses negative string indexing, which is what you really want here
- also, your dataframe is transposed, it would be more normal to have one single column
name
with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
df1 <- df[,grep('0[1-9].$',colnames(df))]
df2 <- df[,-grep('0[1-9].$',colnames(df))]
Quotes in at least two of your answers are incorrect. Please edit.
– Roman Luštrik
Mar 22 at 22:30
Could you elaborate? Or edit and I will accept?
– Wil
Mar 22 at 22:38
1
It was an issue with the keyboard on this device. Do they appear correct to you now?
– Wil
Mar 22 at 22:49
add a comment |
df1 <- df[,grep('0[1-9].$',colnames(df))]
df2 <- df[,-grep('0[1-9].$',colnames(df))]
Quotes in at least two of your answers are incorrect. Please edit.
– Roman Luštrik
Mar 22 at 22:30
Could you elaborate? Or edit and I will accept?
– Wil
Mar 22 at 22:38
1
It was an issue with the keyboard on this device. Do they appear correct to you now?
– Wil
Mar 22 at 22:49
add a comment |
df1 <- df[,grep('0[1-9].$',colnames(df))]
df2 <- df[,-grep('0[1-9].$',colnames(df))]
df1 <- df[,grep('0[1-9].$',colnames(df))]
df2 <- df[,-grep('0[1-9].$',colnames(df))]
edited Mar 22 at 22:48
answered Mar 22 at 22:17
WilWil
1,4371217
1,4371217
Quotes in at least two of your answers are incorrect. Please edit.
– Roman Luštrik
Mar 22 at 22:30
Could you elaborate? Or edit and I will accept?
– Wil
Mar 22 at 22:38
1
It was an issue with the keyboard on this device. Do they appear correct to you now?
– Wil
Mar 22 at 22:49
add a comment |
Quotes in at least two of your answers are incorrect. Please edit.
– Roman Luštrik
Mar 22 at 22:30
Could you elaborate? Or edit and I will accept?
– Wil
Mar 22 at 22:38
1
It was an issue with the keyboard on this device. Do they appear correct to you now?
– Wil
Mar 22 at 22:49
Quotes in at least two of your answers are incorrect. Please edit.
– Roman Luštrik
Mar 22 at 22:30
Quotes in at least two of your answers are incorrect. Please edit.
– Roman Luštrik
Mar 22 at 22:30
Could you elaborate? Or edit and I will accept?
– Wil
Mar 22 at 22:38
Could you elaborate? Or edit and I will accept?
– Wil
Mar 22 at 22:38
1
1
It was an issue with the keyboard on this device. Do they appear correct to you now?
– Wil
Mar 22 at 22:49
It was an issue with the keyboard on this device. Do they appear correct to you now?
– Wil
Mar 22 at 22:49
add a comment |
You could use tidyr::separate(..., last=-1)
approach
- which uses negative string indexing, which is what you really want here
- also, your dataframe is transposed, it would be more normal to have one single column
name
with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string.
add a comment |
You could use tidyr::separate(..., last=-1)
approach
- which uses negative string indexing, which is what you really want here
- also, your dataframe is transposed, it would be more normal to have one single column
name
with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string.
add a comment |
You could use tidyr::separate(..., last=-1)
approach
- which uses negative string indexing, which is what you really want here
- also, your dataframe is transposed, it would be more normal to have one single column
name
with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string.
You could use tidyr::separate(..., last=-1)
approach
- which uses negative string indexing, which is what you really want here
- also, your dataframe is transposed, it would be more normal to have one single column
name
with the names, and numerical columnsa, b, c
. Liket(df)
without the unwanted coercion to string.
answered Apr 10 at 10:42
smcismci
15.8k679110
15.8k679110
add a comment |
add a comment |
Really you want negative string indexing. Also, your dataframe is kind of transposed, it would be more normal to have one single column name with the names, and numerical columns
a, b, c
. Liket(df)
without the unwanted coercion to string. If your dataframe was structured like that, you could usetidyr::separate(..., sep=-1)
– smci
Apr 10 at 10:47