简体   繁体   中英

Apply a function on part of a dataframe in R

I defined a function:

my_func <- function(vector1) 
{ // do something 
vector2 //vector 2 has the same length with vector1 
}

Now, I have a dataframe:

id1 id2 value
1 1 0.6
1 1 0.7
1 1 0.2
1 2 0.4
1 2 0.8

I want to create a new column value2 in the dataframe, by apply the function my_func in each part of dataframe's value, with same id1 and same id2 . It means, I want to call

my_func(dataframe[dataframe$id1 == i & dataframe$id2 == j,]$value)

with all possible i and j . and assign these new values to corresponding rows in new column value2 .

Update:

The output should looks like:

id1 id2 value value2
1 1 0.6 3
1 1 0.7 4
1 1 0.2 9
1 2 0.4 2
1 2 0.5 3
1 2 0.8 4
1 3 0.3 2
...

when the [3, 4, 9] is the result of my_func on the [0.6, 0.7, 0.2], and [2,3,4] is the result of my_func on [0.4, 0.5, 0.8] ...

If we are using 'id1' and 'id2' as grouping variables, we can use either dplyr

library(dplyr)
df1 %>%
    group_by(id1, id2) %>%
    mutate(value2= my_func(value))   

or data.table

library(data.table)
setDT(df1)[, value2:= my_func(value),  by = .(id1, id2)]

you can do it similar to this:

my_func <- function(vector1) 100+vector1
d <- read.table(header=TRUE, text=
'id1 id2 value value2
1 1 0.6 3
1 1 0.7 4
1 1 0.2 9
1 2 0.4 2
1 2 0.5 3
1 2 0.8 4
1 3 0.3 2')
d$v3 <- ave(d$value, d$id1, d$id2, FUN=my_func)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM