Is there an R function for counting same value on a row?
I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)
So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)
If you have any idea to develop, thanks for your time
r dataframe count row
add a comment |
I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)
So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)
If you have any idea to develop, thanks for your time
r dataframe count row
I can't tryrowSums(df == "BF50343")
because there is a lot of different values.
– Alex Germain
Jan 2 at 10:11
Should I try withstr_count()
?
– Alex Germain
Jan 3 at 10:24
add a comment |
I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)
So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)
If you have any idea to develop, thanks for your time
r dataframe count row
I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)
So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:
df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)
If you have any idea to develop, thanks for your time
r dataframe count row
r dataframe count row
edited Jan 2 at 18:22
markus
14.7k11336
14.7k11336
asked Jan 2 at 9:46


Alex GermainAlex Germain
778
778
I can't tryrowSums(df == "BF50343")
because there is a lot of different values.
– Alex Germain
Jan 2 at 10:11
Should I try withstr_count()
?
– Alex Germain
Jan 3 at 10:24
add a comment |
I can't tryrowSums(df == "BF50343")
because there is a lot of different values.
– Alex Germain
Jan 2 at 10:11
Should I try withstr_count()
?
– Alex Germain
Jan 3 at 10:24
I can't try
rowSums(df == "BF50343")
because there is a lot of different values.– Alex Germain
Jan 2 at 10:11
I can't try
rowSums(df == "BF50343")
because there is a lot of different values.– Alex Germain
Jan 2 at 10:11
Should I try with
str_count()
?– Alex Germain
Jan 3 at 10:24
Should I try with
str_count()
?– Alex Germain
Jan 3 at 10:24
add a comment |
1 Answer
1
active
oldest
votes
You can use lapply
to loop over the unique values of your character variables:
cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
vals <- as.character(unique(unlist(df[cols]))) ## unique values
res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
colnames(res) <- vals
df <- cbind(df, res)
add a comment |
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%2f54004149%2fis-there-an-r-function-for-counting-same-value-on-a-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use lapply
to loop over the unique values of your character variables:
cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
vals <- as.character(unique(unlist(df[cols]))) ## unique values
res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
colnames(res) <- vals
df <- cbind(df, res)
add a comment |
You can use lapply
to loop over the unique values of your character variables:
cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
vals <- as.character(unique(unlist(df[cols]))) ## unique values
res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
colnames(res) <- vals
df <- cbind(df, res)
add a comment |
You can use lapply
to loop over the unique values of your character variables:
cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
vals <- as.character(unique(unlist(df[cols]))) ## unique values
res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
colnames(res) <- vals
df <- cbind(df, res)
You can use lapply
to loop over the unique values of your character variables:
cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
vals <- as.character(unique(unlist(df[cols]))) ## unique values
res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
colnames(res) <- vals
df <- cbind(df, res)
answered Jan 9 at 15:24
Balint T.Balint T.
513
513
add a comment |
add a comment |
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%2f54004149%2fis-there-an-r-function-for-counting-same-value-on-a-row%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
I can't try
rowSums(df == "BF50343")
because there is a lot of different values.– Alex Germain
Jan 2 at 10:11
Should I try with
str_count()
?– Alex Germain
Jan 3 at 10:24