Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide list of columns by a second list of columns

Tags:

r

data.table

I have script that generates a data.table with some columns I want to divide by some other columns and store the results in new columns. Here's an example.

library(data.table)
dt <- data.table(V1 = c( 5.553465,  4.989168,  2.563682,  6.987971, 19.220936),
                 V2 = c(4.248335, 19.768138,  3.840026, 17.411003, 17.939368),
                 V3 = c(9.683953, 15.344424, 11.729091,  7.534210,  5.404000),
                 V4 = c(5.949093,  4.553023,  9.765656, 11.211069,  4.085964),
                 V5 = c(11.814671,  5.460138,  2.492230,  1.48792,  8.164280))

list1 <- list(c("V1", "V2", "V3"))
list2 <- list(c("V2", "V4", "V5"))
listRatio <- list(c("rat1","rat2","rat3"))

I have tried a variety of approaches to dividing the values in the list1 elements by the values in the list2 elements, unsuccessfully. Two are below; neither works.

dt[, (listRatio) := list1/list2]
dt[, c("rat1","rat2","rat3") := mapply(dt, function(x,y) x / y, x = c(V1, V2, V3),  y = c(V2, V4, V5))]
like image 218
JerryN Avatar asked Jun 14 '16 03:06

JerryN


People also ask

How do I split a list into multiple columns?

To split a pandas column of lists into multiple columns, create a new dataframe by applying the tolist() function to the column. The following is the syntax. You can also pass the names of new columns resulting from the split as a list. Let's see it action with the help of an example.

How do you split a list into two columns in Python?

Use the str. split() Function to Split Strings Into Two List/Columns in Python Pandas. The string can be saved as a series list or constructed from a single, separated string, multiple column dataframes. Functions used are similar to Python's default split() method, but they can only be applied to a single string.

How do I split one column into multiple columns in pandas?

Split column by delimiter into multiple columns Apply the pandas series str. split() function on the “Address” column and pass the delimiter (comma in this case) on which you want to split the column. Also, make sure to pass True to the expand parameter.


2 Answers

We need to convert the list to vector by using [[ and then get the values of each vector in a list with mget, use Map to divide (/) the corresponding columns of each of the list values and assign it to the vector (listRatio[[1]]).

dt[, (listRatio[[1]]) := Map(`/`, mget(list1[[1]]), mget(list2[[1]]))]
dt
#          V1        V2        V3        V4        V5      rat1      rat2      rat3
#1:  5.553465  4.248335  9.683953  5.949093 11.814671 1.3072098 0.7141147 0.8196549
#2:  4.989168 19.768138 15.344424  4.553023  5.460138 0.2523843 4.3417611 2.8102630
#3:  2.563682  3.840026 11.729091  9.765656  2.492230 0.6676210 0.3932174 4.7062635
#4:  6.987971 17.411003  7.534210 11.211069  1.487920 0.4013537 1.5530190 5.0635854
#5: 19.220936 17.939368  5.404000  4.085964  8.164280 1.0714389 4.3904861 0.6619077

NOTE: As @Frank mentioned in the comments, it is better to create a vector of variables names and not a list.

like image 83
akrun Avatar answered Oct 08 '22 21:10

akrun


By using data.frame function

dt <- data.frame(V1 = c( 5.553465,  4.989168,  2.563682,  6.987971, 19.220936),
                 V2 = c(4.248335, 19.768138,  3.840026, 17.411003, 17.939368),
                 V3 = c(9.683953, 15.344424, 11.729091,  7.534210,  5.404000),
                 V4 = c(5.949093,  4.553023,  9.765656, 11.211069,  4.085964),
                 V5 = c(11.814671,  5.460138,  2.492230,  1.48792,  8.164280))

list1 <- list(dt[,c("V1", "V2", "V3")])
list2 <- list(dt[,c("V2", "V4", "V5")])
dt$rat3 <- dt$rat2 <- dt$rat1 <- ""
dt[, c("rat1","rat2","rat3")] <- unlist(list1)/unlist(list2)

         V1        V2        V3        V4        V5      rat1      rat2      rat3
1  5.553465  4.248335  9.683953  5.949093 11.814671 1.3072098 0.7141147 0.8196549
2  4.989168 19.768138 15.344424  4.553023  5.460138 0.2523843 4.3417611 2.8102630
3  2.563682  3.840026 11.729091  9.765656  2.492230 0.6676210 0.3932174 4.7062635
4  6.987971 17.411003  7.534210 11.211069  1.487920 0.4013537 1.5530190 5.0635854
5 19.220936 17.939368  5.404000  4.085964  8.164280 1.0714389 4.3904861 0.6619077
like image 44
Arun kumar mahesh Avatar answered Oct 08 '22 20:10

Arun kumar mahesh