Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a column to each data frame in a list

Tags:

r

Here's my setup

df1<-data.frame(time=c(1,2,3),y=c(2,3,6))
df2<-data.frame(time=c(1,2,3),y=c(3,4,7))
mylist<-list(df1,df2)

mylist
[[1]]
  time y
1    1 2
2    2 3
3    3 6

[[2]]
  time y
1    1 3
2    2 4
3    3 7

I would like to add a column, ratio to each dataframe, where it's the ratio of the value of y relative to the y value at time 1. It would be equivalent to doing

mylist[[1]]$ratio<-mylist[[1]]$y/mylist[[1]]$y[1]
mylist[[2]]$ratio<-mylist[[2]]$y/mylist[[2]]$y[1]

mylist
[[1]]
  time y ratio
1    1 2   1.0
2    2 3   1.5
3    3 6   3.0

[[2]]
  time y    ratio
1    1 3 1.000000
2    2 4 1.333333
3    3 7 2.333333

Any suggestions on how to do this?

like image 813
Ben Avatar asked Dec 26 '13 17:12

Ben


People also ask

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

To add a new column in R, use cbin() function. This function takes a DataFrame as a first argument and for the second argument, use the vector which creates a new column on the DataFrame. Note that this doesn't update the existing DataFrame instead it returns a copy of the DataFrame after adding a new column.

How do you add a column to a DataFrame in Python?

append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.

How do I add a column to a different DataFrame in pandas?

After extraction, the column needs to be simply added to the second dataframe using join() function. This function needs to be called with reference to the dataframe in which the column has to be added and the variable name which stores the extracted column name has to be passed to it as the argument.


1 Answers

Here is an approach with base R only:

lapply(mylist, transform, ratio = y / y[1])
# [[1]]
#   time y ratio
# 1    1 2   1.0
# 2    2 3   1.5
# 3    3 6   3.0
# 
# [[2]]
#   time y    ratio
# 1    1 3 1.000000
# 2    2 4 1.333333
# 3    3 7 2.333333

It might be easier to understand when written as

lapply(mylist, function(x) transform(x, ratio = y / y[1]))

Also, see ?transform.

like image 69
Julius Vainora Avatar answered Oct 18 '22 00:10

Julius Vainora