How do I mutate a list-column to a common one leaving only the last value when there is a vector in the list?

Carlos Davila

I am trying to use purrr::map_chr to get the last element of the vector in a list-column as the actual value in case that it exists.

THE reproducible example:

library(data.table)
library(purrr)

x <- data.table(one = c("a", "b", "c"), two = list("d", c("e","f","g"), NULL))

I want data as it is but changing my list-column to a common one with "g" as the value for x[2,2]. What I've tryed:

x %>% mutate(two = ifelse(is.null(.$two), map_chr(~NA_character_), map_chr(~last(.))))

The result should be the next one.

# one  two
#  a    d 
#  b    g
#  c    NA

Thaks in advance!

akrun

Here is an option. We can use if/else instead of ifelse here

library(dplyr)
library(tidyr)
x %>% 
   mutate(two = map_chr(two, ~ if(is.null(.x)) NA_character_ else last(.x)))
#   one two
#1   a   d
#2   b   g
#3   c  NA

Or replace the NULL elements with NA and extract the last

x %>% 
   mutate(two = map_chr(two, ~ last(replace(.x, is.null(.), NA))))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make the last column of my output include only each element of the list instead of the whole list?

How do I add only the last element of one vector to the last element of another vector?

How do I select the value of the last row of a specific column of each dataframe in a list using a for loop?

Using case_when, how to mutate a new list-column that nests a vector within?

Power Query: How do I add a specific List/Vector as a Column

How do I get to the Clojure metadata when the var is in a vector or a list?

How do I remove only one instance from a list in Python?

How do I list only one instance of each comment?

MySQL: How do I list one column and sum the other?

How do i convert the list of dicts having common key value pair to a new dict with keys as the common value?

Adding a new column to a list of dataframes, but only select one line of a vector

Append to YAML list - how do I only 'append' the value?

How do I print the maximum value in a column of a multidimensional list?

How can i write a list of list in excel to only one column row by row?

How do I check the common occurrences of one type of column value category for for other remaining column values in R?

How do I make a program stop when one list or another list is the same as a third list?

How do I get the last element of a list?

How do I join two columns in python, whilst one column has a url in every row, and other column has list of last part of the url

apply() and table() return strange list when column only has one value (100%)

dplyr mutate column with nearest value in external list

how to display a value in a column in the list if the value is repeating show only one time using mysql & codeigniter?

How do I imitate having two list of items, while having only one list in css?

How to do sorting when I have null value in a list

How do I convert an array (i.e. list) column to Vector

How do I mutate every third element in a list?

Why is mutate_at not creating a different name for new column when I pass it only one column in vars()?

Why export only last value in list when using writerow function

How do I create a DataFrame from a list so that the list will be shown as a column and not as one single row?

how to push the one value to last in list of value combo box