How i can make a table from one column, having three coded variables to 3 columns with un-coded variable in RStudio. If anyone know some kind of code. I have 2 columns, sample and category, and column where 1 specify = weak, 2 = ok and 3 strong, but now i want a table with 3 columns of weak ok and strong with autofilling, like shown in image:
Hey - here's a more general solution to what I think your problem is, using the tidyverse - it's probably not the most parsimonious, but I'm sure someone else could come up with a better one:
library(tidyverse)
df <- tibble(labels = LETTERS[1:10],
value = sample(1:3, 10, replace = TRUE))
df %>%
mutate(n = 1) %>%
pivot_wider(id_cols = c(labels),
names_from = c(value),
values_from = n,
values_fill = 0) %>%
rename(weak = `1`, ok = `2`, strong = `3`)
Thanks @mattsimmons. How i should use this code, like i tried but it shows some kind of error:
df <- read.csv("F:/cochransQ.csv", stringsAsFactors = F)
df %>%
mutate(n = 1) %>%
pivot_wider(id_cols = c(labels),
names_from = c(value),
values_from = n,
values_fill = 0) %>%
rename(weak = `1`, ok = `2`, strong = `3`)
#> Error in df %>% mutate(n = 1) %>% pivot_wider(id_cols = c(labels), names_from = c(value), : could not find function "%>%"
Created on 2020-09-01 by the reprex package (v0.3.0)
You need to load the tidyverse
package (or at least magrittr
) for the %>%
function.
Since you already have some more modern code which is workable, I'll take a whack at it with a base R solution...
This is(by no means) the best, most readable, or extensible solution, but I have a penchant for single-command Base R solutions which take advantage of little know quirks in the language.
Let's imagine your data.frame is named df
. Then,
set.seed(8675309)
df <- data.frame(product = letters, category = sample(3, 26, TRUE))
head(df)
product category
1 a 3
2 b 2
3 c 3
4 d 1
5 e 3
6 f 1
df <- cbind(df,
t(vapply(df[["category"]],
function(y) {
y == c(weak = 1, ok = 2, strong = 3)
},
integer(3))))
head(df)
product category weak ok strong
1 a 3 0 0 1
2 b 2 0 1 0
3 c 3 0 0 1
4 d 1 1 0 0
5 e 3 0 0 1
6 f 1 1 0 0
Nice!
Also: yes, to be clear, my code doesn’t use the same column names as the frame you provided, which elmstedt has helpfully done.
Hello, I installed required tidyverse package but somehow its still not working:
library(RVAideMemoire)
#> Warning: package 'RVAideMemoire' was built under R version 4.0.2
#> *** Package RVAideMemoire v 0.9-77 ***
library(nonpar)
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.0.2
#> Warning: package 'tidyr' was built under R version 4.0.2
#> Warning: package 'dplyr' was built under R version 4.0.2
library(magrittr)
#> Warning: package 'magrittr' was built under R version 4.0.2
#>
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#>
#> set_names
#> The following object is masked from 'package:tidyr':
#>
#> extract
#> The following object is masked from 'package:RVAideMemoire':
#>
#> mod
df <- read.csv("F:/cochransQ.csv", stringsAsFactors = F)
colnames(df)[1] = "product"
df %>%
mutate(n = 1) %>%
pivot_wider(id_cols = c(labels),
names_from = c(value),
values_from = n,
values_fill = 0) %>%
rename(weak = `1`, ok = `2`, strong = `3`)
#> Error: Can't subset columns that don't exist.
#> x Column `value` doesn't exist.
Created on 2020-09-02 by the reprex package (v0.3.0)
See the error below:
#> Error: Can't subset columns that don't exist.
#> x Column `value` doesn't exist
What columns are in your cochransQ.csv"
?
Hey @sharmachetan - I've led you down a garden path. That's on me!
In your specific example, as given, it should be:
df %>%
mutate(n = 1) %>%
pivot_wider(id_cols = c(product),
names_from = c(category),
values_from = n,
values_fill = 0) %>%
rename(weak = `1`, ok = `2`, strong = `3`)
See how I replaced 'label' with 'product', and 'value' with 'category'? Hopefully this gives you a clearer intuition as to what that bit of code is doing. You'll still have to do everything else, including loading the tidyverse package.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.