How to write an algebraic function specific to multiple values across multiple variables?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have two datasets, as follows (in simplified form, believe it or not):
df1 <- structure(list(TERMAC_Value = c(122L, 122L, 122L, 122L, 122L,
605L), TERMAC_ShortName = structure(c(20L, 20L, 20L, 20L, 20L,
17L), .Label = c("AG", "BWS", "DAWS2", "DEMF", "DM", "EAAW",
"EABT", "EASDS", "EASM", "EEAW2", "KSP", "Marsh", "MAWS", "MEMF",
"MeWG", "MWS", "PV", "TWS", "WM", "ZM"), class = "factor"), GLOBCOV_Value = c(30L,
130L, 60L, 140L, 140L, 60L), Glob_ShortName = structure(c(14L,
11L, 8L, 6L, 6L, 8L), .Label = c("Bare", "CldBdFrst", "CropVeg",
"FloodFrt", "FloodGrs", "Grass", "GrassSgrub", "OpBdFrst", "OpNdFrst",
"RainCrop", "Shrub", "ShrubGrass", "Sparse", "VegCrop", "Water"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
df2 <- structure(list(x = structure(c(1L, 2L, 6L, 7L, 3L, 4L, 5L), .Label = c("b1[1] VegCrop",
"b1[2] Shrub", "b1b2[1,1] VegCrop ZM", "b1b2[1,2] VegCrop PV",
"b1b2[2,2] Shrub PV", "b2[1] ZM", "b2[2] PV"), class = "factor"),
Mean = c(4.0188615, 1.0145846, -0.3134498, 1.1346487, -0.2701251,
-0.5978043, -0.06)), class = "data.frame", row.names = c(NA,
-7L))
My problem is that I need to calculate a unique value following this formula:
y = 4.05 + b1[i] + b2[i] + b1b2[i,i]
This would be an additional column on df1. So for example, the value for ZM and VegCrop would be as follows:
y = 4.05 - 0.3 + 4 - 0.27
y = 4.23
So my new dataset would be:
TERMAC_Value TERMAC_ShortName GLOBCOV_Value Glob_ShortName VALUE
122 ZM 30 VegCrop 4.23
122 ZM 130 Shrub etc
122 ZM 60 OpBdFrst ...
122 ZM 140 Grass ...
122 ZM 140 Grass ...
605 PV 60 OpBdFrst ...
I know this can be easily accomplished with some for
looping across the datasets, but as I have done my best to avoid dealing with for
loops, I'm now stuck.
Thanks in advance for any help.
r
add a comment |
I have two datasets, as follows (in simplified form, believe it or not):
df1 <- structure(list(TERMAC_Value = c(122L, 122L, 122L, 122L, 122L,
605L), TERMAC_ShortName = structure(c(20L, 20L, 20L, 20L, 20L,
17L), .Label = c("AG", "BWS", "DAWS2", "DEMF", "DM", "EAAW",
"EABT", "EASDS", "EASM", "EEAW2", "KSP", "Marsh", "MAWS", "MEMF",
"MeWG", "MWS", "PV", "TWS", "WM", "ZM"), class = "factor"), GLOBCOV_Value = c(30L,
130L, 60L, 140L, 140L, 60L), Glob_ShortName = structure(c(14L,
11L, 8L, 6L, 6L, 8L), .Label = c("Bare", "CldBdFrst", "CropVeg",
"FloodFrt", "FloodGrs", "Grass", "GrassSgrub", "OpBdFrst", "OpNdFrst",
"RainCrop", "Shrub", "ShrubGrass", "Sparse", "VegCrop", "Water"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
df2 <- structure(list(x = structure(c(1L, 2L, 6L, 7L, 3L, 4L, 5L), .Label = c("b1[1] VegCrop",
"b1[2] Shrub", "b1b2[1,1] VegCrop ZM", "b1b2[1,2] VegCrop PV",
"b1b2[2,2] Shrub PV", "b2[1] ZM", "b2[2] PV"), class = "factor"),
Mean = c(4.0188615, 1.0145846, -0.3134498, 1.1346487, -0.2701251,
-0.5978043, -0.06)), class = "data.frame", row.names = c(NA,
-7L))
My problem is that I need to calculate a unique value following this formula:
y = 4.05 + b1[i] + b2[i] + b1b2[i,i]
This would be an additional column on df1. So for example, the value for ZM and VegCrop would be as follows:
y = 4.05 - 0.3 + 4 - 0.27
y = 4.23
So my new dataset would be:
TERMAC_Value TERMAC_ShortName GLOBCOV_Value Glob_ShortName VALUE
122 ZM 30 VegCrop 4.23
122 ZM 130 Shrub etc
122 ZM 60 OpBdFrst ...
122 ZM 140 Grass ...
122 ZM 140 Grass ...
605 PV 60 OpBdFrst ...
I know this can be easily accomplished with some for
looping across the datasets, but as I have done my best to avoid dealing with for
loops, I'm now stuck.
Thanks in advance for any help.
r
1
The example you gave for AM and VegCrop is hard to understand. How did you come up with those values for b1, b2, and b1b2?
– mickey
Nov 16 '18 at 16:40
What exactly isi
supposed to represent? How did your data get in this way in the first place? The data indf2
seems to be in a very unfriendly format. What do you do with values forOpBdFrst
which don't seem to be represented indf2
?
– MrFlick
Nov 16 '18 at 16:44
@mickey those are values from a Bayes model (run using JAGS) estimating abundance of a species according to that particular predictor. I'm trying to create a (very) rough heat map of abundance using those values (obviously some problems theoretically with this, but I want it in place for later tweaking and refinement)
– E.O.
Nov 17 '18 at 8:23
@MrFlick I was using "i" there as a representation of, for example, b1 ZM, b1 PV, and etc. As you note, the full dataset has many categories. And my apologies, it's somewhat confusing because of the way I cut the datasets. You're correct that in the full dataset there is OpBdFrst, etc, following on from ZM and PV. For the purposes of this example, I tried to keep it simple and focused on the set up of the formula into which all of those predictors will eventually be plugged in. Hopefully that makes sense.
– E.O.
Nov 17 '18 at 8:26
add a comment |
I have two datasets, as follows (in simplified form, believe it or not):
df1 <- structure(list(TERMAC_Value = c(122L, 122L, 122L, 122L, 122L,
605L), TERMAC_ShortName = structure(c(20L, 20L, 20L, 20L, 20L,
17L), .Label = c("AG", "BWS", "DAWS2", "DEMF", "DM", "EAAW",
"EABT", "EASDS", "EASM", "EEAW2", "KSP", "Marsh", "MAWS", "MEMF",
"MeWG", "MWS", "PV", "TWS", "WM", "ZM"), class = "factor"), GLOBCOV_Value = c(30L,
130L, 60L, 140L, 140L, 60L), Glob_ShortName = structure(c(14L,
11L, 8L, 6L, 6L, 8L), .Label = c("Bare", "CldBdFrst", "CropVeg",
"FloodFrt", "FloodGrs", "Grass", "GrassSgrub", "OpBdFrst", "OpNdFrst",
"RainCrop", "Shrub", "ShrubGrass", "Sparse", "VegCrop", "Water"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
df2 <- structure(list(x = structure(c(1L, 2L, 6L, 7L, 3L, 4L, 5L), .Label = c("b1[1] VegCrop",
"b1[2] Shrub", "b1b2[1,1] VegCrop ZM", "b1b2[1,2] VegCrop PV",
"b1b2[2,2] Shrub PV", "b2[1] ZM", "b2[2] PV"), class = "factor"),
Mean = c(4.0188615, 1.0145846, -0.3134498, 1.1346487, -0.2701251,
-0.5978043, -0.06)), class = "data.frame", row.names = c(NA,
-7L))
My problem is that I need to calculate a unique value following this formula:
y = 4.05 + b1[i] + b2[i] + b1b2[i,i]
This would be an additional column on df1. So for example, the value for ZM and VegCrop would be as follows:
y = 4.05 - 0.3 + 4 - 0.27
y = 4.23
So my new dataset would be:
TERMAC_Value TERMAC_ShortName GLOBCOV_Value Glob_ShortName VALUE
122 ZM 30 VegCrop 4.23
122 ZM 130 Shrub etc
122 ZM 60 OpBdFrst ...
122 ZM 140 Grass ...
122 ZM 140 Grass ...
605 PV 60 OpBdFrst ...
I know this can be easily accomplished with some for
looping across the datasets, but as I have done my best to avoid dealing with for
loops, I'm now stuck.
Thanks in advance for any help.
r
I have two datasets, as follows (in simplified form, believe it or not):
df1 <- structure(list(TERMAC_Value = c(122L, 122L, 122L, 122L, 122L,
605L), TERMAC_ShortName = structure(c(20L, 20L, 20L, 20L, 20L,
17L), .Label = c("AG", "BWS", "DAWS2", "DEMF", "DM", "EAAW",
"EABT", "EASDS", "EASM", "EEAW2", "KSP", "Marsh", "MAWS", "MEMF",
"MeWG", "MWS", "PV", "TWS", "WM", "ZM"), class = "factor"), GLOBCOV_Value = c(30L,
130L, 60L, 140L, 140L, 60L), Glob_ShortName = structure(c(14L,
11L, 8L, 6L, 6L, 8L), .Label = c("Bare", "CldBdFrst", "CropVeg",
"FloodFrt", "FloodGrs", "Grass", "GrassSgrub", "OpBdFrst", "OpNdFrst",
"RainCrop", "Shrub", "ShrubGrass", "Sparse", "VegCrop", "Water"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
df2 <- structure(list(x = structure(c(1L, 2L, 6L, 7L, 3L, 4L, 5L), .Label = c("b1[1] VegCrop",
"b1[2] Shrub", "b1b2[1,1] VegCrop ZM", "b1b2[1,2] VegCrop PV",
"b1b2[2,2] Shrub PV", "b2[1] ZM", "b2[2] PV"), class = "factor"),
Mean = c(4.0188615, 1.0145846, -0.3134498, 1.1346487, -0.2701251,
-0.5978043, -0.06)), class = "data.frame", row.names = c(NA,
-7L))
My problem is that I need to calculate a unique value following this formula:
y = 4.05 + b1[i] + b2[i] + b1b2[i,i]
This would be an additional column on df1. So for example, the value for ZM and VegCrop would be as follows:
y = 4.05 - 0.3 + 4 - 0.27
y = 4.23
So my new dataset would be:
TERMAC_Value TERMAC_ShortName GLOBCOV_Value Glob_ShortName VALUE
122 ZM 30 VegCrop 4.23
122 ZM 130 Shrub etc
122 ZM 60 OpBdFrst ...
122 ZM 140 Grass ...
122 ZM 140 Grass ...
605 PV 60 OpBdFrst ...
I know this can be easily accomplished with some for
looping across the datasets, but as I have done my best to avoid dealing with for
loops, I'm now stuck.
Thanks in advance for any help.
r
r
asked Nov 16 '18 at 16:34
E.O.E.O.
6511
6511
1
The example you gave for AM and VegCrop is hard to understand. How did you come up with those values for b1, b2, and b1b2?
– mickey
Nov 16 '18 at 16:40
What exactly isi
supposed to represent? How did your data get in this way in the first place? The data indf2
seems to be in a very unfriendly format. What do you do with values forOpBdFrst
which don't seem to be represented indf2
?
– MrFlick
Nov 16 '18 at 16:44
@mickey those are values from a Bayes model (run using JAGS) estimating abundance of a species according to that particular predictor. I'm trying to create a (very) rough heat map of abundance using those values (obviously some problems theoretically with this, but I want it in place for later tweaking and refinement)
– E.O.
Nov 17 '18 at 8:23
@MrFlick I was using "i" there as a representation of, for example, b1 ZM, b1 PV, and etc. As you note, the full dataset has many categories. And my apologies, it's somewhat confusing because of the way I cut the datasets. You're correct that in the full dataset there is OpBdFrst, etc, following on from ZM and PV. For the purposes of this example, I tried to keep it simple and focused on the set up of the formula into which all of those predictors will eventually be plugged in. Hopefully that makes sense.
– E.O.
Nov 17 '18 at 8:26
add a comment |
1
The example you gave for AM and VegCrop is hard to understand. How did you come up with those values for b1, b2, and b1b2?
– mickey
Nov 16 '18 at 16:40
What exactly isi
supposed to represent? How did your data get in this way in the first place? The data indf2
seems to be in a very unfriendly format. What do you do with values forOpBdFrst
which don't seem to be represented indf2
?
– MrFlick
Nov 16 '18 at 16:44
@mickey those are values from a Bayes model (run using JAGS) estimating abundance of a species according to that particular predictor. I'm trying to create a (very) rough heat map of abundance using those values (obviously some problems theoretically with this, but I want it in place for later tweaking and refinement)
– E.O.
Nov 17 '18 at 8:23
@MrFlick I was using "i" there as a representation of, for example, b1 ZM, b1 PV, and etc. As you note, the full dataset has many categories. And my apologies, it's somewhat confusing because of the way I cut the datasets. You're correct that in the full dataset there is OpBdFrst, etc, following on from ZM and PV. For the purposes of this example, I tried to keep it simple and focused on the set up of the formula into which all of those predictors will eventually be plugged in. Hopefully that makes sense.
– E.O.
Nov 17 '18 at 8:26
1
1
The example you gave for AM and VegCrop is hard to understand. How did you come up with those values for b1, b2, and b1b2?
– mickey
Nov 16 '18 at 16:40
The example you gave for AM and VegCrop is hard to understand. How did you come up with those values for b1, b2, and b1b2?
– mickey
Nov 16 '18 at 16:40
What exactly is
i
supposed to represent? How did your data get in this way in the first place? The data in df2
seems to be in a very unfriendly format. What do you do with values for OpBdFrst
which don't seem to be represented in df2
?– MrFlick
Nov 16 '18 at 16:44
What exactly is
i
supposed to represent? How did your data get in this way in the first place? The data in df2
seems to be in a very unfriendly format. What do you do with values for OpBdFrst
which don't seem to be represented in df2
?– MrFlick
Nov 16 '18 at 16:44
@mickey those are values from a Bayes model (run using JAGS) estimating abundance of a species according to that particular predictor. I'm trying to create a (very) rough heat map of abundance using those values (obviously some problems theoretically with this, but I want it in place for later tweaking and refinement)
– E.O.
Nov 17 '18 at 8:23
@mickey those are values from a Bayes model (run using JAGS) estimating abundance of a species according to that particular predictor. I'm trying to create a (very) rough heat map of abundance using those values (obviously some problems theoretically with this, but I want it in place for later tweaking and refinement)
– E.O.
Nov 17 '18 at 8:23
@MrFlick I was using "i" there as a representation of, for example, b1 ZM, b1 PV, and etc. As you note, the full dataset has many categories. And my apologies, it's somewhat confusing because of the way I cut the datasets. You're correct that in the full dataset there is OpBdFrst, etc, following on from ZM and PV. For the purposes of this example, I tried to keep it simple and focused on the set up of the formula into which all of those predictors will eventually be plugged in. Hopefully that makes sense.
– E.O.
Nov 17 '18 at 8:26
@MrFlick I was using "i" there as a representation of, for example, b1 ZM, b1 PV, and etc. As you note, the full dataset has many categories. And my apologies, it's somewhat confusing because of the way I cut the datasets. You're correct that in the full dataset there is OpBdFrst, etc, following on from ZM and PV. For the purposes of this example, I tried to keep it simple and focused on the set up of the formula into which all of those predictors will eventually be plugged in. Hopefully that makes sense.
– E.O.
Nov 17 '18 at 8:26
add a comment |
0
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53341952%2fhow-to-write-an-algebraic-function-specific-to-multiple-values-across-multiple-v%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
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.
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%2f53341952%2fhow-to-write-an-algebraic-function-specific-to-multiple-values-across-multiple-v%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
1
The example you gave for AM and VegCrop is hard to understand. How did you come up with those values for b1, b2, and b1b2?
– mickey
Nov 16 '18 at 16:40
What exactly is
i
supposed to represent? How did your data get in this way in the first place? The data indf2
seems to be in a very unfriendly format. What do you do with values forOpBdFrst
which don't seem to be represented indf2
?– MrFlick
Nov 16 '18 at 16:44
@mickey those are values from a Bayes model (run using JAGS) estimating abundance of a species according to that particular predictor. I'm trying to create a (very) rough heat map of abundance using those values (obviously some problems theoretically with this, but I want it in place for later tweaking and refinement)
– E.O.
Nov 17 '18 at 8:23
@MrFlick I was using "i" there as a representation of, for example, b1 ZM, b1 PV, and etc. As you note, the full dataset has many categories. And my apologies, it's somewhat confusing because of the way I cut the datasets. You're correct that in the full dataset there is OpBdFrst, etc, following on from ZM and PV. For the purposes of this example, I tried to keep it simple and focused on the set up of the formula into which all of those predictors will eventually be plugged in. Hopefully that makes sense.
– E.O.
Nov 17 '18 at 8:26