Function for creating relative frequency tables for each variable
I have a dataset with 220 variables (columns). My data look like this
test var1 var2 var3
1 Pretest No Sometimes No
2 Postest No Sometimes No
3 Pretest No Yes No
4 Postest No Yes No
5 Pretest No Sometimes No
6 Postest No Yes No
7 Pretest No Yes No
8 Postest No No No
9 Pretest No Yes No
10 Postest No Sometimes No
11 Pretest No Sometimes No
12 Postest No No No
13 Pretest Yes Yes No
14 Postest No Sometimes No
15 Pretest No No No
16 Postest No Sometimes No
17 Pretest No Sometimes No
18 Postest No No No
19 Pretest No Yes No
20 Postest No Yes No
For each variable I want to produce a relative frequency table by group. To that end, I'm using dplyr
propvar1 <- dataprepost %>%
group_by(test,var1) %>%
summarise(n = n()) %>%
mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
For instance, this gives me the table I want for Var1:
test var1 n rel.freq
1 Postest Sometimes 120 14%
2 Postest No 667 79%
3 Postest Yes 44 5%
4 Postest NA 10 1%
5 Pretest Sometimes 155 18%
6 Pretest No 623 74%
7 Pretest Yes 49 6%
8 Pretest NA 14 2%
How can I create a function to do this automatically for each variable in the data set, so I end up with a similar table for each variable?
r function dplyr
add a comment |
I have a dataset with 220 variables (columns). My data look like this
test var1 var2 var3
1 Pretest No Sometimes No
2 Postest No Sometimes No
3 Pretest No Yes No
4 Postest No Yes No
5 Pretest No Sometimes No
6 Postest No Yes No
7 Pretest No Yes No
8 Postest No No No
9 Pretest No Yes No
10 Postest No Sometimes No
11 Pretest No Sometimes No
12 Postest No No No
13 Pretest Yes Yes No
14 Postest No Sometimes No
15 Pretest No No No
16 Postest No Sometimes No
17 Pretest No Sometimes No
18 Postest No No No
19 Pretest No Yes No
20 Postest No Yes No
For each variable I want to produce a relative frequency table by group. To that end, I'm using dplyr
propvar1 <- dataprepost %>%
group_by(test,var1) %>%
summarise(n = n()) %>%
mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
For instance, this gives me the table I want for Var1:
test var1 n rel.freq
1 Postest Sometimes 120 14%
2 Postest No 667 79%
3 Postest Yes 44 5%
4 Postest NA 10 1%
5 Pretest Sometimes 155 18%
6 Pretest No 623 74%
7 Pretest Yes 49 6%
8 Pretest NA 14 2%
How can I create a function to do this automatically for each variable in the data set, so I end up with a similar table for each variable?
r function dplyr
Please make your example reproducible by including sample data and matching expected output.
– Maurits Evers
Nov 13 '18 at 6:33
add a comment |
I have a dataset with 220 variables (columns). My data look like this
test var1 var2 var3
1 Pretest No Sometimes No
2 Postest No Sometimes No
3 Pretest No Yes No
4 Postest No Yes No
5 Pretest No Sometimes No
6 Postest No Yes No
7 Pretest No Yes No
8 Postest No No No
9 Pretest No Yes No
10 Postest No Sometimes No
11 Pretest No Sometimes No
12 Postest No No No
13 Pretest Yes Yes No
14 Postest No Sometimes No
15 Pretest No No No
16 Postest No Sometimes No
17 Pretest No Sometimes No
18 Postest No No No
19 Pretest No Yes No
20 Postest No Yes No
For each variable I want to produce a relative frequency table by group. To that end, I'm using dplyr
propvar1 <- dataprepost %>%
group_by(test,var1) %>%
summarise(n = n()) %>%
mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
For instance, this gives me the table I want for Var1:
test var1 n rel.freq
1 Postest Sometimes 120 14%
2 Postest No 667 79%
3 Postest Yes 44 5%
4 Postest NA 10 1%
5 Pretest Sometimes 155 18%
6 Pretest No 623 74%
7 Pretest Yes 49 6%
8 Pretest NA 14 2%
How can I create a function to do this automatically for each variable in the data set, so I end up with a similar table for each variable?
r function dplyr
I have a dataset with 220 variables (columns). My data look like this
test var1 var2 var3
1 Pretest No Sometimes No
2 Postest No Sometimes No
3 Pretest No Yes No
4 Postest No Yes No
5 Pretest No Sometimes No
6 Postest No Yes No
7 Pretest No Yes No
8 Postest No No No
9 Pretest No Yes No
10 Postest No Sometimes No
11 Pretest No Sometimes No
12 Postest No No No
13 Pretest Yes Yes No
14 Postest No Sometimes No
15 Pretest No No No
16 Postest No Sometimes No
17 Pretest No Sometimes No
18 Postest No No No
19 Pretest No Yes No
20 Postest No Yes No
For each variable I want to produce a relative frequency table by group. To that end, I'm using dplyr
propvar1 <- dataprepost %>%
group_by(test,var1) %>%
summarise(n = n()) %>%
mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
For instance, this gives me the table I want for Var1:
test var1 n rel.freq
1 Postest Sometimes 120 14%
2 Postest No 667 79%
3 Postest Yes 44 5%
4 Postest NA 10 1%
5 Pretest Sometimes 155 18%
6 Pretest No 623 74%
7 Pretest Yes 49 6%
8 Pretest NA 14 2%
How can I create a function to do this automatically for each variable in the data set, so I end up with a similar table for each variable?
r function dplyr
r function dplyr
edited Nov 13 '18 at 14:19
Chris
asked Nov 13 '18 at 4:50
ChrisChris
577
577
Please make your example reproducible by including sample data and matching expected output.
– Maurits Evers
Nov 13 '18 at 6:33
add a comment |
Please make your example reproducible by including sample data and matching expected output.
– Maurits Evers
Nov 13 '18 at 6:33
Please make your example reproducible by including sample data and matching expected output.
– Maurits Evers
Nov 13 '18 at 6:33
Please make your example reproducible by including sample data and matching expected output.
– Maurits Evers
Nov 13 '18 at 6:33
add a comment |
1 Answer
1
active
oldest
votes
Not the most elegant piece of code... But it works!
Data
d <- structure(list(test = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Postest",
"Pretest"), class = "factor"), var1 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("No", "Yes"), class = "factor"), var2 = structure(c(2L,
2L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 2L,
1L, 3L, 3L), .Label = c("No", "Sometimes", "Yes"), class = "factor"),
var3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "No", class = "factor")), .Names = c("test",
"var1", "var2", "var3"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"))`
Solution
output <- NULL
for (t_cat in unique(d$test))
x <- d[d$test == t_cat, ]
x$test <- NULL
l <- lapply(x, function(x)
output <- data.frame(table(x))
colnames(output) <- c('val', 'n')
output$rel.freq <- output$n/length(x)
output$category <- t_cat
return(output)
)
r <- do.call("rbind", lapply(names(l), function(x) cbind(l[[x]], 'var' = x)))
output <- rbind(output, r)
print(output)
# val n rel.freq category var
# 1 No 9 0.9 Pretest var1
# 2 Yes 1 0.1 Pretest var1
# 3 No 1 0.1 Pretest var2
# 4 Sometimes 4 0.4 Pretest var2
# 5 Yes 5 0.5 Pretest var2
# 6 No 10 1.0 Pretest var3
# 7 No 10 1.0 Postest var1
# 8 Yes 0 0.0 Postest var1
# 9 No 3 0.3 Postest var2
# 10 Sometimes 4 0.4 Postest var2
# 11 Yes 3 0.3 Postest var2
# 12 No 10 1.0 Postest var3
Thanks. The function should group by test and variable, then calculate n, and then calculate the relative frecuency of each variable. This function seems to work only with one variable.
– Chris
Nov 13 '18 at 15:00
Fixed answer, see above!
– 12b345b6b78
Nov 15 '18 at 0:27
Thank you very much @12b345b6b78 This is exactly what I wanted to do. But just one more thing: How can I change the function If I wanted to group by more than one variable, for instance, 'test' and 'school'?
– Chris
Nov 17 '18 at 11:02
Unfortunately, the only suggestion I have is to keep wrapping it in for loops (looping through unique values in 'test', 'school' etc), while not forgetting to alter the innermost subsetd[d$test == t_cat, ]
such that it represents a slice of original data that only has the specific 'test', 'school', etc inherited from the loops
– 12b345b6b78
Nov 17 '18 at 17:44
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%2f53274017%2ffunction-for-creating-relative-frequency-tables-for-each-variable%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
Not the most elegant piece of code... But it works!
Data
d <- structure(list(test = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Postest",
"Pretest"), class = "factor"), var1 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("No", "Yes"), class = "factor"), var2 = structure(c(2L,
2L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 2L,
1L, 3L, 3L), .Label = c("No", "Sometimes", "Yes"), class = "factor"),
var3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "No", class = "factor")), .Names = c("test",
"var1", "var2", "var3"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"))`
Solution
output <- NULL
for (t_cat in unique(d$test))
x <- d[d$test == t_cat, ]
x$test <- NULL
l <- lapply(x, function(x)
output <- data.frame(table(x))
colnames(output) <- c('val', 'n')
output$rel.freq <- output$n/length(x)
output$category <- t_cat
return(output)
)
r <- do.call("rbind", lapply(names(l), function(x) cbind(l[[x]], 'var' = x)))
output <- rbind(output, r)
print(output)
# val n rel.freq category var
# 1 No 9 0.9 Pretest var1
# 2 Yes 1 0.1 Pretest var1
# 3 No 1 0.1 Pretest var2
# 4 Sometimes 4 0.4 Pretest var2
# 5 Yes 5 0.5 Pretest var2
# 6 No 10 1.0 Pretest var3
# 7 No 10 1.0 Postest var1
# 8 Yes 0 0.0 Postest var1
# 9 No 3 0.3 Postest var2
# 10 Sometimes 4 0.4 Postest var2
# 11 Yes 3 0.3 Postest var2
# 12 No 10 1.0 Postest var3
Thanks. The function should group by test and variable, then calculate n, and then calculate the relative frecuency of each variable. This function seems to work only with one variable.
– Chris
Nov 13 '18 at 15:00
Fixed answer, see above!
– 12b345b6b78
Nov 15 '18 at 0:27
Thank you very much @12b345b6b78 This is exactly what I wanted to do. But just one more thing: How can I change the function If I wanted to group by more than one variable, for instance, 'test' and 'school'?
– Chris
Nov 17 '18 at 11:02
Unfortunately, the only suggestion I have is to keep wrapping it in for loops (looping through unique values in 'test', 'school' etc), while not forgetting to alter the innermost subsetd[d$test == t_cat, ]
such that it represents a slice of original data that only has the specific 'test', 'school', etc inherited from the loops
– 12b345b6b78
Nov 17 '18 at 17:44
add a comment |
Not the most elegant piece of code... But it works!
Data
d <- structure(list(test = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Postest",
"Pretest"), class = "factor"), var1 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("No", "Yes"), class = "factor"), var2 = structure(c(2L,
2L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 2L,
1L, 3L, 3L), .Label = c("No", "Sometimes", "Yes"), class = "factor"),
var3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "No", class = "factor")), .Names = c("test",
"var1", "var2", "var3"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"))`
Solution
output <- NULL
for (t_cat in unique(d$test))
x <- d[d$test == t_cat, ]
x$test <- NULL
l <- lapply(x, function(x)
output <- data.frame(table(x))
colnames(output) <- c('val', 'n')
output$rel.freq <- output$n/length(x)
output$category <- t_cat
return(output)
)
r <- do.call("rbind", lapply(names(l), function(x) cbind(l[[x]], 'var' = x)))
output <- rbind(output, r)
print(output)
# val n rel.freq category var
# 1 No 9 0.9 Pretest var1
# 2 Yes 1 0.1 Pretest var1
# 3 No 1 0.1 Pretest var2
# 4 Sometimes 4 0.4 Pretest var2
# 5 Yes 5 0.5 Pretest var2
# 6 No 10 1.0 Pretest var3
# 7 No 10 1.0 Postest var1
# 8 Yes 0 0.0 Postest var1
# 9 No 3 0.3 Postest var2
# 10 Sometimes 4 0.4 Postest var2
# 11 Yes 3 0.3 Postest var2
# 12 No 10 1.0 Postest var3
Thanks. The function should group by test and variable, then calculate n, and then calculate the relative frecuency of each variable. This function seems to work only with one variable.
– Chris
Nov 13 '18 at 15:00
Fixed answer, see above!
– 12b345b6b78
Nov 15 '18 at 0:27
Thank you very much @12b345b6b78 This is exactly what I wanted to do. But just one more thing: How can I change the function If I wanted to group by more than one variable, for instance, 'test' and 'school'?
– Chris
Nov 17 '18 at 11:02
Unfortunately, the only suggestion I have is to keep wrapping it in for loops (looping through unique values in 'test', 'school' etc), while not forgetting to alter the innermost subsetd[d$test == t_cat, ]
such that it represents a slice of original data that only has the specific 'test', 'school', etc inherited from the loops
– 12b345b6b78
Nov 17 '18 at 17:44
add a comment |
Not the most elegant piece of code... But it works!
Data
d <- structure(list(test = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Postest",
"Pretest"), class = "factor"), var1 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("No", "Yes"), class = "factor"), var2 = structure(c(2L,
2L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 2L,
1L, 3L, 3L), .Label = c("No", "Sometimes", "Yes"), class = "factor"),
var3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "No", class = "factor")), .Names = c("test",
"var1", "var2", "var3"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"))`
Solution
output <- NULL
for (t_cat in unique(d$test))
x <- d[d$test == t_cat, ]
x$test <- NULL
l <- lapply(x, function(x)
output <- data.frame(table(x))
colnames(output) <- c('val', 'n')
output$rel.freq <- output$n/length(x)
output$category <- t_cat
return(output)
)
r <- do.call("rbind", lapply(names(l), function(x) cbind(l[[x]], 'var' = x)))
output <- rbind(output, r)
print(output)
# val n rel.freq category var
# 1 No 9 0.9 Pretest var1
# 2 Yes 1 0.1 Pretest var1
# 3 No 1 0.1 Pretest var2
# 4 Sometimes 4 0.4 Pretest var2
# 5 Yes 5 0.5 Pretest var2
# 6 No 10 1.0 Pretest var3
# 7 No 10 1.0 Postest var1
# 8 Yes 0 0.0 Postest var1
# 9 No 3 0.3 Postest var2
# 10 Sometimes 4 0.4 Postest var2
# 11 Yes 3 0.3 Postest var2
# 12 No 10 1.0 Postest var3
Not the most elegant piece of code... But it works!
Data
d <- structure(list(test = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Postest",
"Pretest"), class = "factor"), var1 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("No", "Yes"), class = "factor"), var2 = structure(c(2L,
2L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 2L,
1L, 3L, 3L), .Label = c("No", "Sometimes", "Yes"), class = "factor"),
var3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "No", class = "factor")), .Names = c("test",
"var1", "var2", "var3"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"))`
Solution
output <- NULL
for (t_cat in unique(d$test))
x <- d[d$test == t_cat, ]
x$test <- NULL
l <- lapply(x, function(x)
output <- data.frame(table(x))
colnames(output) <- c('val', 'n')
output$rel.freq <- output$n/length(x)
output$category <- t_cat
return(output)
)
r <- do.call("rbind", lapply(names(l), function(x) cbind(l[[x]], 'var' = x)))
output <- rbind(output, r)
print(output)
# val n rel.freq category var
# 1 No 9 0.9 Pretest var1
# 2 Yes 1 0.1 Pretest var1
# 3 No 1 0.1 Pretest var2
# 4 Sometimes 4 0.4 Pretest var2
# 5 Yes 5 0.5 Pretest var2
# 6 No 10 1.0 Pretest var3
# 7 No 10 1.0 Postest var1
# 8 Yes 0 0.0 Postest var1
# 9 No 3 0.3 Postest var2
# 10 Sometimes 4 0.4 Postest var2
# 11 Yes 3 0.3 Postest var2
# 12 No 10 1.0 Postest var3
edited Nov 15 '18 at 0:26
answered Nov 13 '18 at 4:53
12b345b6b7812b345b6b78
782115
782115
Thanks. The function should group by test and variable, then calculate n, and then calculate the relative frecuency of each variable. This function seems to work only with one variable.
– Chris
Nov 13 '18 at 15:00
Fixed answer, see above!
– 12b345b6b78
Nov 15 '18 at 0:27
Thank you very much @12b345b6b78 This is exactly what I wanted to do. But just one more thing: How can I change the function If I wanted to group by more than one variable, for instance, 'test' and 'school'?
– Chris
Nov 17 '18 at 11:02
Unfortunately, the only suggestion I have is to keep wrapping it in for loops (looping through unique values in 'test', 'school' etc), while not forgetting to alter the innermost subsetd[d$test == t_cat, ]
such that it represents a slice of original data that only has the specific 'test', 'school', etc inherited from the loops
– 12b345b6b78
Nov 17 '18 at 17:44
add a comment |
Thanks. The function should group by test and variable, then calculate n, and then calculate the relative frecuency of each variable. This function seems to work only with one variable.
– Chris
Nov 13 '18 at 15:00
Fixed answer, see above!
– 12b345b6b78
Nov 15 '18 at 0:27
Thank you very much @12b345b6b78 This is exactly what I wanted to do. But just one more thing: How can I change the function If I wanted to group by more than one variable, for instance, 'test' and 'school'?
– Chris
Nov 17 '18 at 11:02
Unfortunately, the only suggestion I have is to keep wrapping it in for loops (looping through unique values in 'test', 'school' etc), while not forgetting to alter the innermost subsetd[d$test == t_cat, ]
such that it represents a slice of original data that only has the specific 'test', 'school', etc inherited from the loops
– 12b345b6b78
Nov 17 '18 at 17:44
Thanks. The function should group by test and variable, then calculate n, and then calculate the relative frecuency of each variable. This function seems to work only with one variable.
– Chris
Nov 13 '18 at 15:00
Thanks. The function should group by test and variable, then calculate n, and then calculate the relative frecuency of each variable. This function seems to work only with one variable.
– Chris
Nov 13 '18 at 15:00
Fixed answer, see above!
– 12b345b6b78
Nov 15 '18 at 0:27
Fixed answer, see above!
– 12b345b6b78
Nov 15 '18 at 0:27
Thank you very much @12b345b6b78 This is exactly what I wanted to do. But just one more thing: How can I change the function If I wanted to group by more than one variable, for instance, 'test' and 'school'?
– Chris
Nov 17 '18 at 11:02
Thank you very much @12b345b6b78 This is exactly what I wanted to do. But just one more thing: How can I change the function If I wanted to group by more than one variable, for instance, 'test' and 'school'?
– Chris
Nov 17 '18 at 11:02
Unfortunately, the only suggestion I have is to keep wrapping it in for loops (looping through unique values in 'test', 'school' etc), while not forgetting to alter the innermost subset
d[d$test == t_cat, ]
such that it represents a slice of original data that only has the specific 'test', 'school', etc inherited from the loops– 12b345b6b78
Nov 17 '18 at 17:44
Unfortunately, the only suggestion I have is to keep wrapping it in for loops (looping through unique values in 'test', 'school' etc), while not forgetting to alter the innermost subset
d[d$test == t_cat, ]
such that it represents a slice of original data that only has the specific 'test', 'school', etc inherited from the loops– 12b345b6b78
Nov 17 '18 at 17:44
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%2f53274017%2ffunction-for-creating-relative-frequency-tables-for-each-variable%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
Please make your example reproducible by including sample data and matching expected output.
– Maurits Evers
Nov 13 '18 at 6:33