Home > Net >  R: return dataframe from function with for loop
R: return dataframe from function with for loop

Time:11-11

With looping over the dataframe data:

data <- data.frame(q1 = c("2017-10-11", NA, NA), 
q2 = c(NA, "2014-11-11", "2017-10-13"))

I want to create a new dataframe that is counting the rows not containing NAs in each column.

This way it worked:

df <- data %>%
mutate(c = nrow(data) - sapply(data, function(i) sum(is.na(i))))

But I want to write function in order to make it more reproducible:

func <- function(data){
y <- data_old$v1
x <- vector()
nr <- nrow(data)
for(i in colnames(data)){
x[i] <- nr - sum(is.na(data$i))
}
df <- data.frame(y, x = (x))
return(df)
}

But I am just getting a vector of NAs. What am I doing wrong?

The output is supposed to look like this:

df <- data.frame(y = c("q1", "q2"), x = c(1, 2))

CodePudding user response:

data <- data.frame(q1 = c("2017-10-11", NA, NA), 
                   q2 = c(NA, "2014-11-11", "2017-10-13"))
  
func <- function(data)  {
  data.frame(y = colnames(data),
             x = apply(data, 2, function(x) sum(is.na(x))))  
}

func(data) |> print()
    y x
q1 q1 2
q2 q2 1
  • Related