[R] loop through columns in a data frame
jim holtman
jho|tm@n @end|ng |rom gm@||@com
Mon Mar 25 19:06:42 CET 2019
R Notebook
You forgot to provide what your test data looks like. For example, are all
the columns a single letter followed by “_" as the name, or are there
longer names? Are there always matched pairs (‘le’ and ‘me’) or can singles
occur?
Hide
library(tidyverse)# create some data
test <- tibble(a_le = sample(3, 10, TRUE),
a_me = sample(3, 10, TRUE),
b_le = sample(3, 10, TRUE),
b_me = sample(3, 10, TRUE),
long_le = sample(3, 10, TRUE),
long_me = sample(3, 10, TRUE),
short_le = sample(3, 10, TRUE)
)
So get the names of the columns that contain ‘le’ or ‘me’ and group them
together for processing
Hide
col_names <- grep("_(le|me)$", names(test), value = TRUE)
group <- tibble(id = str_remove(col_names, "_.*"), col = col_names)
result <- group %>%
group_by(id) %>%
do(tibble(x = rowSums(test[, .$col] == 1)))# add new columns backfor
(i in split(result, result$id)){
test[, paste0(i$id[1], "_new")] <- as.integer(i$x > 0)
}
test
a_le
<int>
a_me
<int>
b_le
<int>
b_me
<int>
long_le
<int>
long_me
<int>
short_le
<int>
a_new
<int>
b_new
<int>
long_new
<int>
3 1 2 3 1 2 2 1 0 1
2 3 3 2 1 1 1 0 0 1
3 2 3 2 1 3 3 0 0 1
2 3 1 3 3 1 2 0 1 1
1 1 2 1 1 2 2 1 1 1
3 3 3 1 1 1 1 0 1 1
1 2 1 2 2 2 2 1 1 0
1 3 2 3 1 1 3 1 0 1
3 1 1 1 3 3 2 1 1 0
1 1 1 2 3 3 3 1 1 0
1-10 of 10 rows | 1-10 of 11 columns
Jim Holtman
*Data Munger Guru*
*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*
On Mon, Mar 25, 2019 at 10:08 AM Yuan, Keming (CDC/DDNID/NCIPC/DVP) via
R-help <r-help using r-project.org> wrote:
> Hi All,
>
> I have a data frame with variable names like A_le, A_me, B_le, B_me, C_le,
> C_me....
> if A_le=1 or A_me=1 then I need to create a new column A_new=1. Same
> operation to create columns B_new, C_new...
> Does anyone know how to use loop (or other methods) to create new columns?
> In SAS, I can use array to get it done. But I don't know how to do it in R.
>
> Thanks,
>
> Keming Yuan
> CDC
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list