How to get a specific value out of a dataframe in R











up vote
0
down vote

favorite












I am trying to get the cp value for which my xerroris the smallest. I fitted a model like below and printed the cptable. Since the list is so long, I displayed only the first 20 rows.



>printcp(RT_model)

Regression tree:
rpart(formula = BW ~ ., data = train, method = "anova",
control = rpart.control(minsplit = 0, minbucket = 1, cp = -1))

Variables actually used in tree construction:
[1] age black boy cigar collgrad hsgrad married
natal2 natal3 nosmoke novisit somecoll wtgain

Root node error: 5.2267e+10/159689 = 327304

n= 159689

CP nsplit rel error xerror xstd
1 4.3760e-02 0 1.00000 1.00001 0.0054277
2 1.6392e-02 1 0.95624 0.95661 0.0050844
3 1.1851e-02 2 0.93985 0.94067 0.0049671
4 1.1133e-02 3 0.92800 0.92446 0.0049150
5 1.0735e-02 4 0.91686 0.91709 0.0048956
6 6.1850e-03 5 0.90613 0.90692 0.0048695
7 3.3414e-03 6 0.89994 0.90054 0.0048561
8 2.6481e-03 7 0.89660 0.89680 0.0048502
9 2.4185e-03 8 0.89395 0.89441 0.0048449
10 2.1499e-03 9 0.89154 0.89232 0.0048248
11 2.0960e-03 10 0.88939 0.88993 0.0048055
12 1.3600e-03 11 0.88729 0.88822 0.0048031
13 1.3513e-03 12 0.88593 0.88616 0.0047898
14 1.2209e-03 13 0.88458 0.88600 0.0047862
15 9.2359e-04 14 0.88336 0.88454 0.0047731
16 9.1119e-04 15 0.88243 0.88364 0.0047679
17 7.8948e-04 16 0.88152 0.88300 0.0047662
18 7.4059e-04 17 0.88073 0.88221 0.0047638
19 6.8623e-04 18 0.87999 0.88142 0.0047610
20 6.7196e-04 19 0.87931 0.88077 0.0047620
...
[ erreichte getOption("max.print") -- 25545 Zeilen ausgelassen ]


Since there are so many values:



To get the cpwith the smalles xerror, I use the following code:



   minCP <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "CP"] 


The corresponding xerror of the minCP equals: 0.8690961 xerror



Now I want to apply a rule called "minimal xerror + standard deviation"



The corresponding standard deviation of minCP equals 0.004700524 xstd



I need the greatest value of the table that is equal or smaller than the new xerror called xerror_new (0.8690961 xerror + 0.004700524 xstd = 0.8737966). So I need the greatest value of the xerrorcolumn of the cptable which is less or equal to 0.8737966



How do I get the cp-value of the cptable which has an xerror smaller or equal to the xerror_newvalue of 0.8737966?



I tried the following but it failed hard.



  min_xerror <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), 
"xerror"]


this gave me the minimal xerrorin the table. And the following gives me the corresponding minimal xstd



   min_xstd <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "xstd"]


here I apply the "minimal xerror + standard deviation" rule:



  xerror_new <- min_xerror + min_xstd


up to here I get values and everything is fine. From here I struggle:



  xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= 
min_xerror_xstd] & RT_model$cptable["xerror" >= min_xerror]), "xerror"]


or I tried this:



 xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= min_xerror_xstd]), "xerror"]


Both yield no result.



How can I get this value? Thanks.










share|improve this question
























  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).
    – desertnaut
    Nov 22 at 9:19






  • 1




    My suggestion is that you use the broomstick package to convert the results into a data.frame and use dplyr to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
    – José
    Nov 22 at 9:50

















up vote
0
down vote

favorite












I am trying to get the cp value for which my xerroris the smallest. I fitted a model like below and printed the cptable. Since the list is so long, I displayed only the first 20 rows.



>printcp(RT_model)

Regression tree:
rpart(formula = BW ~ ., data = train, method = "anova",
control = rpart.control(minsplit = 0, minbucket = 1, cp = -1))

Variables actually used in tree construction:
[1] age black boy cigar collgrad hsgrad married
natal2 natal3 nosmoke novisit somecoll wtgain

Root node error: 5.2267e+10/159689 = 327304

n= 159689

CP nsplit rel error xerror xstd
1 4.3760e-02 0 1.00000 1.00001 0.0054277
2 1.6392e-02 1 0.95624 0.95661 0.0050844
3 1.1851e-02 2 0.93985 0.94067 0.0049671
4 1.1133e-02 3 0.92800 0.92446 0.0049150
5 1.0735e-02 4 0.91686 0.91709 0.0048956
6 6.1850e-03 5 0.90613 0.90692 0.0048695
7 3.3414e-03 6 0.89994 0.90054 0.0048561
8 2.6481e-03 7 0.89660 0.89680 0.0048502
9 2.4185e-03 8 0.89395 0.89441 0.0048449
10 2.1499e-03 9 0.89154 0.89232 0.0048248
11 2.0960e-03 10 0.88939 0.88993 0.0048055
12 1.3600e-03 11 0.88729 0.88822 0.0048031
13 1.3513e-03 12 0.88593 0.88616 0.0047898
14 1.2209e-03 13 0.88458 0.88600 0.0047862
15 9.2359e-04 14 0.88336 0.88454 0.0047731
16 9.1119e-04 15 0.88243 0.88364 0.0047679
17 7.8948e-04 16 0.88152 0.88300 0.0047662
18 7.4059e-04 17 0.88073 0.88221 0.0047638
19 6.8623e-04 18 0.87999 0.88142 0.0047610
20 6.7196e-04 19 0.87931 0.88077 0.0047620
...
[ erreichte getOption("max.print") -- 25545 Zeilen ausgelassen ]


Since there are so many values:



To get the cpwith the smalles xerror, I use the following code:



   minCP <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "CP"] 


The corresponding xerror of the minCP equals: 0.8690961 xerror



Now I want to apply a rule called "minimal xerror + standard deviation"



The corresponding standard deviation of minCP equals 0.004700524 xstd



I need the greatest value of the table that is equal or smaller than the new xerror called xerror_new (0.8690961 xerror + 0.004700524 xstd = 0.8737966). So I need the greatest value of the xerrorcolumn of the cptable which is less or equal to 0.8737966



How do I get the cp-value of the cptable which has an xerror smaller or equal to the xerror_newvalue of 0.8737966?



I tried the following but it failed hard.



  min_xerror <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), 
"xerror"]


this gave me the minimal xerrorin the table. And the following gives me the corresponding minimal xstd



   min_xstd <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "xstd"]


here I apply the "minimal xerror + standard deviation" rule:



  xerror_new <- min_xerror + min_xstd


up to here I get values and everything is fine. From here I struggle:



  xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= 
min_xerror_xstd] & RT_model$cptable["xerror" >= min_xerror]), "xerror"]


or I tried this:



 xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= min_xerror_xstd]), "xerror"]


Both yield no result.



How can I get this value? Thanks.










share|improve this question
























  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).
    – desertnaut
    Nov 22 at 9:19






  • 1




    My suggestion is that you use the broomstick package to convert the results into a data.frame and use dplyr to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
    – José
    Nov 22 at 9:50















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to get the cp value for which my xerroris the smallest. I fitted a model like below and printed the cptable. Since the list is so long, I displayed only the first 20 rows.



>printcp(RT_model)

Regression tree:
rpart(formula = BW ~ ., data = train, method = "anova",
control = rpart.control(minsplit = 0, minbucket = 1, cp = -1))

Variables actually used in tree construction:
[1] age black boy cigar collgrad hsgrad married
natal2 natal3 nosmoke novisit somecoll wtgain

Root node error: 5.2267e+10/159689 = 327304

n= 159689

CP nsplit rel error xerror xstd
1 4.3760e-02 0 1.00000 1.00001 0.0054277
2 1.6392e-02 1 0.95624 0.95661 0.0050844
3 1.1851e-02 2 0.93985 0.94067 0.0049671
4 1.1133e-02 3 0.92800 0.92446 0.0049150
5 1.0735e-02 4 0.91686 0.91709 0.0048956
6 6.1850e-03 5 0.90613 0.90692 0.0048695
7 3.3414e-03 6 0.89994 0.90054 0.0048561
8 2.6481e-03 7 0.89660 0.89680 0.0048502
9 2.4185e-03 8 0.89395 0.89441 0.0048449
10 2.1499e-03 9 0.89154 0.89232 0.0048248
11 2.0960e-03 10 0.88939 0.88993 0.0048055
12 1.3600e-03 11 0.88729 0.88822 0.0048031
13 1.3513e-03 12 0.88593 0.88616 0.0047898
14 1.2209e-03 13 0.88458 0.88600 0.0047862
15 9.2359e-04 14 0.88336 0.88454 0.0047731
16 9.1119e-04 15 0.88243 0.88364 0.0047679
17 7.8948e-04 16 0.88152 0.88300 0.0047662
18 7.4059e-04 17 0.88073 0.88221 0.0047638
19 6.8623e-04 18 0.87999 0.88142 0.0047610
20 6.7196e-04 19 0.87931 0.88077 0.0047620
...
[ erreichte getOption("max.print") -- 25545 Zeilen ausgelassen ]


Since there are so many values:



To get the cpwith the smalles xerror, I use the following code:



   minCP <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "CP"] 


The corresponding xerror of the minCP equals: 0.8690961 xerror



Now I want to apply a rule called "minimal xerror + standard deviation"



The corresponding standard deviation of minCP equals 0.004700524 xstd



I need the greatest value of the table that is equal or smaller than the new xerror called xerror_new (0.8690961 xerror + 0.004700524 xstd = 0.8737966). So I need the greatest value of the xerrorcolumn of the cptable which is less or equal to 0.8737966



How do I get the cp-value of the cptable which has an xerror smaller or equal to the xerror_newvalue of 0.8737966?



I tried the following but it failed hard.



  min_xerror <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), 
"xerror"]


this gave me the minimal xerrorin the table. And the following gives me the corresponding minimal xstd



   min_xstd <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "xstd"]


here I apply the "minimal xerror + standard deviation" rule:



  xerror_new <- min_xerror + min_xstd


up to here I get values and everything is fine. From here I struggle:



  xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= 
min_xerror_xstd] & RT_model$cptable["xerror" >= min_xerror]), "xerror"]


or I tried this:



 xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= min_xerror_xstd]), "xerror"]


Both yield no result.



How can I get this value? Thanks.










share|improve this question















I am trying to get the cp value for which my xerroris the smallest. I fitted a model like below and printed the cptable. Since the list is so long, I displayed only the first 20 rows.



>printcp(RT_model)

Regression tree:
rpart(formula = BW ~ ., data = train, method = "anova",
control = rpart.control(minsplit = 0, minbucket = 1, cp = -1))

Variables actually used in tree construction:
[1] age black boy cigar collgrad hsgrad married
natal2 natal3 nosmoke novisit somecoll wtgain

Root node error: 5.2267e+10/159689 = 327304

n= 159689

CP nsplit rel error xerror xstd
1 4.3760e-02 0 1.00000 1.00001 0.0054277
2 1.6392e-02 1 0.95624 0.95661 0.0050844
3 1.1851e-02 2 0.93985 0.94067 0.0049671
4 1.1133e-02 3 0.92800 0.92446 0.0049150
5 1.0735e-02 4 0.91686 0.91709 0.0048956
6 6.1850e-03 5 0.90613 0.90692 0.0048695
7 3.3414e-03 6 0.89994 0.90054 0.0048561
8 2.6481e-03 7 0.89660 0.89680 0.0048502
9 2.4185e-03 8 0.89395 0.89441 0.0048449
10 2.1499e-03 9 0.89154 0.89232 0.0048248
11 2.0960e-03 10 0.88939 0.88993 0.0048055
12 1.3600e-03 11 0.88729 0.88822 0.0048031
13 1.3513e-03 12 0.88593 0.88616 0.0047898
14 1.2209e-03 13 0.88458 0.88600 0.0047862
15 9.2359e-04 14 0.88336 0.88454 0.0047731
16 9.1119e-04 15 0.88243 0.88364 0.0047679
17 7.8948e-04 16 0.88152 0.88300 0.0047662
18 7.4059e-04 17 0.88073 0.88221 0.0047638
19 6.8623e-04 18 0.87999 0.88142 0.0047610
20 6.7196e-04 19 0.87931 0.88077 0.0047620
...
[ erreichte getOption("max.print") -- 25545 Zeilen ausgelassen ]


Since there are so many values:



To get the cpwith the smalles xerror, I use the following code:



   minCP <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "CP"] 


The corresponding xerror of the minCP equals: 0.8690961 xerror



Now I want to apply a rule called "minimal xerror + standard deviation"



The corresponding standard deviation of minCP equals 0.004700524 xstd



I need the greatest value of the table that is equal or smaller than the new xerror called xerror_new (0.8690961 xerror + 0.004700524 xstd = 0.8737966). So I need the greatest value of the xerrorcolumn of the cptable which is less or equal to 0.8737966



How do I get the cp-value of the cptable which has an xerror smaller or equal to the xerror_newvalue of 0.8737966?



I tried the following but it failed hard.



  min_xerror <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), 
"xerror"]


this gave me the minimal xerrorin the table. And the following gives me the corresponding minimal xstd



   min_xstd <- RT_model$cptable[which.min(RT_model$cptable[,"xerror"]), "xstd"]


here I apply the "minimal xerror + standard deviation" rule:



  xerror_new <- min_xerror + min_xstd


up to here I get values and everything is fine. From here I struggle:



  xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= 
min_xerror_xstd] & RT_model$cptable["xerror" >= min_xerror]), "xerror"]


or I tried this:



 xerror_plus_xstd <- RT_model$cptable[which.max(RT_model$cptable["xerror"<= min_xerror_xstd]), "xerror"]


Both yield no result.



How can I get this value? Thanks.







r dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:19









desertnaut

15.7k53463




15.7k53463










asked Nov 22 at 8:07









Milan Mitrovic

11




11












  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).
    – desertnaut
    Nov 22 at 9:19






  • 1




    My suggestion is that you use the broomstick package to convert the results into a data.frame and use dplyr to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
    – José
    Nov 22 at 9:50




















  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).
    – desertnaut
    Nov 22 at 9:19






  • 1




    My suggestion is that you use the broomstick package to convert the results into a data.frame and use dplyr to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
    – José
    Nov 22 at 9:50


















Question has nothing to do with machine-learning - kindly do not spam the tag (removed).
– desertnaut
Nov 22 at 9:19




Question has nothing to do with machine-learning - kindly do not spam the tag (removed).
– desertnaut
Nov 22 at 9:19




1




1




My suggestion is that you use the broomstick package to convert the results into a data.frame and use dplyr to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
– José
Nov 22 at 9:50






My suggestion is that you use the broomstick package to convert the results into a data.frame and use dplyr to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
– José
Nov 22 at 9:50



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426368%2fhow-to-get-a-specific-value-out-of-a-dataframe-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426368%2fhow-to-get-a-specific-value-out-of-a-dataframe-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo