1
df=data.frame(Name=c("leonard","andrey","richard","john"), #initial data
           Func=c("FUNC1","FUNC2","FUNC3","FUNC1"))
df=split(df,df$Func) # split by Func column
sapply(df,function(x){length(df$x)}) # error here attempt 1
sapply(df,function(x){nrow(df$x)}) # error attempt 2 give NULL result

I would like to know the number of observations that exist in each data.frame in this list

Expected output:


> data.frame(FUNC1=2,FUNC2=1,FUNC3=1)
  func1 func2 func3
      2     1     1
KmnsE2
  • 424
  • 2
  • 9

3 Answers3

2

You can use nrow in sapply.

sapply(df, nrow)
#sapply(df,function(x){length(x$Name)}) #Alternative using your 1sd try
#sapply(df,function(x){nrow(x)}) #Alternative using your 2nd try
#FUNC1 FUNC2 FUNC3 
#    2     1     1 
GKi
  • 37,245
  • 2
  • 26
  • 48
1

Like this?

> table(df$Func)

FUNC1 FUNC2 FUNC3 
    2     1     1 
BellmanEqn
  • 791
  • 3
  • 11
1

A lapply() approach would be:

#Data
df=data.frame(Name=c("leonard","andrey","richard","john"), #initial data
              Func=c("FUNC1","FUNC2","FUNC3","FUNC1"))
df=split(df,df$Func) # split by Func column
#Lapply approach
lapply(df,function(x) dim(x)[1])

Output:

$FUNC1
[1] 2

$FUNC2
[1] 1

$FUNC3
[1] 1
Duck
  • 39,058
  • 13
  • 42
  • 84