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 xerror
is 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 cp
with 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 xerror
column 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_new
value 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 xerror
in 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
add a comment |
up vote
0
down vote
favorite
I am trying to get the cp
value for which my xerror
is 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 cp
with 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 xerror
column 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_new
value 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 xerror
in 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
Question has nothing to do withmachine-learning
- kindly do not spam the tag (removed).
– desertnaut
Nov 22 at 9:19
1
My suggestion is that you use thebroomstick
package to convert the results into a data.frame and usedplyr
to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
– José
Nov 22 at 9:50
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to get the cp
value for which my xerror
is 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 cp
with 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 xerror
column 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_new
value 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 xerror
in 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
I am trying to get the cp
value for which my xerror
is 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 cp
with 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 xerror
column 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_new
value 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 xerror
in 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
r dataframe
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 withmachine-learning
- kindly do not spam the tag (removed).
– desertnaut
Nov 22 at 9:19
1
My suggestion is that you use thebroomstick
package to convert the results into a data.frame and usedplyr
to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.
– José
Nov 22 at 9:50
add a comment |
Question has nothing to do withmachine-learning
- kindly do not spam the tag (removed).
– desertnaut
Nov 22 at 9:19
1
My suggestion is that you use thebroomstick
package to convert the results into a data.frame and usedplyr
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 usedplyr
to carry out the selection. You also coud share a reproducible example, so someone can help more effectively.– José
Nov 22 at 9:50