Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: adding new column using lapply

Tags:

r

I want to add a column to each dataframe of a list, where the values of two columns (col1 and col2) are concatenated

# create new column #
original.list <- lapply(original.list, cbind, new_column = c(""))
# add data to new column
new.list <- lapply(original.list, function (x) x$new_column = paste(x$col1, x$col2, sep = "_"))

However, this seems to be wrong since the result is a list with only the new column.

This is how each data frame of my original.list looks like

col1    col2
name1   1
name2   2
name3   3

This is how each data frame of my new.list should look like

col1    col2    new_column
name1   1       name1_1
name2   2       name2_2
name3   3       name3_3
like image 757
user1987607 Avatar asked Apr 26 '15 12:04

user1987607


People also ask

How do I add a column to a list in R?

To add a new column in the R data frame, use the $ operator. Assign vector to the new column, which ultimately adds a new column with five rows in the data frame. In this code, mando is our data frame, character_bounty is our new column name, added in the data frame, and rv_new is a vector that is a column value.

Can you use Lapply on a DataFrame in R?

In R Programming Language to apply a function to every integer type value in a data frame, we can use lapply function from dplyr package. And if the datatype of values is string then we can use paste() with lapply.

Can you use Lapply on a DataFrame?

lapply() on a data frame. If, instead of a list, you had a data frame of stock returns, could you still use lapply() ? Yes! Perhaps surprisingly, data frames are actually lists under the hood, and an lapply() call would apply the function to each column of the data frame.

How do you use lapply on a column in R?

Using lapply on certain columns of an R data frame Consider that you have a data frame and you want to multiply the elements of the first column by one, the elements of the second by two and so on. On the one hand, for all columns you could write: df <- data.frame(x = c(6, 2), y = c(3, 6), z = c(2, 3)) lapply(1:ncol(df), function(i) df i] * i)

How to use the lapply function on a data frame?

On the one hand, for all columns you could write: On the other hand, If you want to use the lapply function to certain columns of the data frame you could type: If needed, you can nest multiply lapply functions. Consider that you want to iterate over the columns and rows of a data frame and apply a function to each cell.

How do I use lapply () function in Python?

Use the lapply () function when you want to apply a function to each element of a list, vector, or data frame and obtain a list as a result. The basic syntax for the lapply () function is as follows: The following code illustrates several examples of using lapply () on the columns of a data frame.

How to return a vector with the lapply function?

Analogous to the previous, you can return a vector with the lapply function using the unlist or simplify2array functions as follows: Consider that you have a data frame and you want to multiply the elements of the first column by one, the elements of the second by two and so on.


1 Answers

your example is not reproducible, but if you want x and not just the new column maybe you should return x from the function...

 z <- data.frame(a=1:10)
 xlist <- list(z,z,z,z)
# wrong
 lapply(xlist, function(x) { x$b <- rep(8,10)})
#correct
lapply(xlist, function(x) { x$b <- rep(8,10);return(x)})
like image 107
user1317221_G Avatar answered Oct 05 '22 17:10

user1317221_G