How can I fill columns based on values in another column?

erikfjonsson

I have a large dataframe containing a cross table of keys from other tables. Instead of having multiple instances of key1 coupled with different values for key2 I would like there to be one row for each key1 with several columns instead.

I tried doing this with a for loop but it couldn't get it to work.

Here's an example. I have a data frame with the structure df1 and I would like it to have the structure of df2.

df1 <- data.frame(c("a", "a", "a", "b", "b", "c", "c", "c", "c", "c", "d"),c(1, 2, 3, 2, 3, 1, 2, 3, 4, 5, 9))
names(df1) <- c("key1", "key2")


df2 <- data.frame(c("a", "b", "c", "d"), c(1, 2, 1, 9), c(2, 3, 2, NA), c(3, NA, 3, NA), c(NA, NA, 4, NA), c(NA, NA, 5, NA))
names(df2) <- c("key1", "key2_1", "key2_2", "key2_3", "key2_4", "key2_5")

I suspect this is possible using an approach utilizing apply but I haven't found a way yet. Any help is appreciated!

Adam Quek
library(dplyr)
library(tidyr)

df1 %>% 
  group_by(key1) %>% 
  mutate(var = paste0("key2_", seq(n()))) %>% 
  spread(var, key2)

# # A tibble: 4 x 6
# # Groups:   key1 [4]
#     key1  key2_1 key2_2 key2_3 key2_4 key2_5
#     <fct>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
#   1 a          1      2      3     NA     NA
#   2 b          2      3     NA     NA     NA
#   3 c          1      2      3      4      5
#   4 d          9     NA     NA     NA     NA

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I create a new column of values based on the grouped sum of values from two other columns?

Fill new column in one dataframe with values from another, based on values in two other columns? (Python/Pandas)

Create columns based on unique column values and fill

How do I fill in values for columns based on matching few other column's row values in R

How to fill empty values in a dataframe based on columns in another dataframe?

How can I filter for pandas columns or rows based on values of another column?

In pandas, how do I create columns out of unique values in one column, and then fill it based on values in another column?

How to create a column and fill it with new values based on a separate columns values

How to move values into new columns based on values in another column

How can I subtract values within one column based on values in mutliple other columns?

How can I create order numbers based on the values of another column?

How can I sum the values in one column based on the floor'd value of another column in pandas?

How can I sum values in column based on the value in another column in Awk

Fill down column based on values in another

How can I loop though some columns and all rows, and if the value is nan, fill that value with values of other column?

How to fill columns based on other column values?

How to fill the NA values based on another column in python

In Excel, how can I automatically fill value in multiple columns based on contents of another sheet?

How can I fill a column based on a difference between the values of two different columns, using groupby?

Fill in column based on values in other columns pandas

Pyspark: How to fill null values based on value on another column

Fill empty columns with values from another column of another row based on an identifier

VBA Excel - Fill columns based on values from another column and conditional

How to fill a columns based on the null values in another column in pandas

How to fill in blanks based off another column's values in R

How do I fill in values of a column based on multiple columns in R?

Pandas- how can I iterate through a list to fill a column based on a condition in another column?

Fill columns based on groups and conditions in another column

Pandas: (By groups based on 1 column) How to both forward fill and backward fill a column based on the values in another column