Applying a function in each row of a data frame in R

I would like to apply some function to every row of data in R.

A function can return a single-line data frame or nothing (I think 'return ()' returns nothing?).

I would like to apply this function to each of the rows of a given data frame and get the resulting data frame (which is possibly shorter, that is, has fewer rows than the original).

For example, if the original data framework looks something like this:

id size name
1  100  dave
2  200  sarah
3  50   ben

And the function I use gets the string n in the dataframe (ie, a single-line DataFrame), returns it as it is, if the name rhymes with "brave", otherwise returns null, then the result should be:

id size name
1  100  dave

, , , , (, ) . , ( , $size>100, , , boo(single_row_df).

P.s. , , - apply(df, MARGIN=1), do.call(rbind ...), , , ( Error in do.call(rbind, filterd) : second argument must be a list)

UPDATE

:

ranges.filter <- function(ranges,boo) {
    subset(x=ranges,subset=!any(boo[start:end]))
}

ranges.filter , :

start end
100   200
250   400
698   1520
1988  2147
...

(TRUE,FALSE,TRUE,TRUE,TRUE,...)

, TRUE . , 100 .. 200 , FALSE 100 .. 200.

, , , numerical expression has 53 elements: only the first used.

+3
3

, lapply apply, .

> rhymesWithBrave <- function(x) substring(x,nchar(x)-2) =="ave"
> do.call(rbind,lapply(1:nrow(dfr),function(i,dfr)
+                      if(rhymesWithBrave(dfr[i,"name"])) dfr[i,] else NULL,
+                      dfr))
  id size name
1  1  100 dave

subset :

> subset(dfr,rhymesWithBrave(name))
  id size name
1  1  100 dave

, lapply :

> add100tosize <- function(x) within(x,size <- size+100)
> do.call(rbind,lapply(1:nrow(dfr),function(i,dfr)
+                      if(rhymesWithBrave(dfr[i,"name"])) add100tosize(dfr[i,])
+                      else NULL,dfr))
  id size name
1  1  200 dave

, , subset.

> add100tosize(subset(dfr,rhymesWithBrave(name)))
  id size name
1  1  200 dave

UPDATE:

, , (: / TRUE 1s, FALSE 0s)

test <- function(x)
  rowSums(mapply(function(start,end,x) x >= start & x <= end,
                 start=c(100,250,698,1988),
                 end=c(200,400,1520,2147))) == 0

subset(dfr,test(size))
+1

plyr CRAN , , ddply.

install.packages(plyr)
library(plyr)
help(ddply)

, , .

...

> d
    x          y           z xx
1   1 0.68434946 0.643786918  8
2   2 0.64429292 0.231382912  5
3   3 0.15106083 0.307459540  3
4   4 0.65725669 0.553340712  5
5   5 0.02981373 0.736611949  4
6   6 0.83895251 0.845043443  4
7   7 0.22788855 0.606439470  4
8   8 0.88663285 0.048965094  9
9   9 0.44768780 0.009275935  9
10 10 0.23954606 0.356021488  4

sd x , "xx":

> ddply(d,"xx",function(r){data.frame(mean=mean(r$x),sd=sd(r$x))})
  xx mean        sd
1  3  3.0        NA
2  4  7.0 2.1602469
3  5  3.0 1.4142136
4  8  1.0        NA
5  9  8.5 0.7071068

, .

+4

, subset:

subset(orig.df,grepl("ave",name))

The second argument evaluates to a boolean expression that determines which rows are stored. You can force this expression to use the values ​​from the number of columns as much as you like, for examplegrepl("ave",name) & size>50

0
source

Source: https://habr.com/ru/post/1763434/


All Articles