-1

I have a dataframe with one column of 0 and 1, labeled "capture". I would like to convert it to Y and N but keep it in the dataframe which I'm later using to print a kable. How could I successfully mix variable types like this?

ï..video capture point     x     y   c.2..cm. speed..cm.s. speedB..cm.s.
1       23       0     1 4.972 1.138         NA           NA            NA
2       23       0     2 4.970 1.145 0.00728011    0.2184033      6.552099
3       23       0     3 4.989 1.154 0.02102380    0.6307139     18.921416
4       23       0     4 4.973 1.140 0.02126029    0.6378087     19.134262
5       23       0     5 4.993 1.145 0.02061553    0.6184658     18.553975
6       23       0     6 5.000 1.129 0.01746425    0.5239275     15.717824

Here is the code for my kable as well, if that is helpful.

reduced.rounded.summary %>%
  kable(caption = "Multiple Copepod Interactions", font_size = 12) %>%
  kable_styling(full_width = F, position = "float_right") %>%
  add_header_above(c(" " = 2, "Mouth" = 3))

2 Answers2

0

If you have a column of 0 and 1, then adding 1 to it gives a column of 1 and 2s. These can be used to subscript the simple vector c("No", "Yes"):

reduced.rounded.summary %>% mutate(capture = c("No", "Yes")[capture + 1])
#>   ï..video capture point     x     y   c.2..cm. speed..cm.s. speedB..cm.s.
#> 1       23      No     1 4.972 1.138         NA           NA            NA
#> 2       23      No     2 4.970 1.145 0.00728011    0.2184033      6.552099
#> 3       23      No     3 4.989 1.154 0.02102380    0.6307139     18.921416
#> 4       23      No     4 4.973 1.140 0.02126029    0.6378087     19.134262
#> 5       23      No     5 4.993 1.145 0.02061553    0.6184658     18.553975
#> 6       23      No     6 5.000 1.129 0.01746425    0.5239275     15.717824
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks, Allan. That seems like it should work, but it gives me an error saying "invalid subscript type 'list'" – Betsy Potter Jul 21 '20 at 15:08
  • @BetsyPotter the example in my answer is just taking the data you provided in your example and calling it `reduced.rounded.summary`. Can you confirm that this is indeed a data frame and that it has a column called `capture` that is numeric? – Allan Cameron Jul 21 '20 at 15:29
  • this is only the head of the dataframe with over 1000 rows. the dataframe does have a column called "capture" that is numeric with only 0s and 1s. – Betsy Potter Jul 21 '20 at 17:37
  • @BetsyPotter so what do you get if you do `reduced.rounded.summary$capture + 1` ? You should get a numeric vector of 1s and 2s. – Allan Cameron Jul 21 '20 at 17:43
  • a series of 1s and 2s are printed. – Betsy Potter Jul 21 '20 at 18:06
  • @Betsy so what if you do `c("Yes", "No")[reduced.rounded.summary$capture + 1]` – Allan Cameron Jul 21 '20 at 18:09
  • "character(0)" prints, but the data doesn't change – Betsy Potter Jul 21 '20 at 18:29
  • @BetsyPotter you can't have an empty character vector with the exact line (copied and pasted) `c("Yes", "No")[reduced.rounded.summary$capture + 1] ` unless `reduced.rounded.summary$capture` does not exist. Can you try pasting `reduced.rounded.summary$capture` into your console, confirming it exists, and then copying and pasting `c("Yes", "No")[reduced.rounded.summary$capture + 1]` – Allan Cameron Jul 21 '20 at 18:42
0

A simple ifelse operation will do:

df <- data.frame(
  capture = c(0,1,1,0,1,0,0,0,1,0)
)

df$capture <- ifelse(df$capture==1, "yes", "no")
df
   capture
1       no
2      yes
3      yes
4       no
5      yes
6       no
7       no
8       no
9      yes
10      no
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34