Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Data frame output not matching expected output

$
0
0

I have a function which takes a data frame and gives basic summary statistics. My problem is, the function output is not matching the expected output.

# create my data frame
x = c(55.3846, 54.5385, 54.1538, 54.8205, 54.7692, 54.7179)
y = c(47.1795, 47.0256, 47.4872, 47.4103, 47.3333, 47.8718)
df = data.frame(x,y)

# create function to create summary statistics
xy_stats <- function(data) {
x_mean <- mean(data$x)
y_mean <- mean(data$y)
x_sd <- sd(data$x)
y_sd <- sd(data$y)
corr <- cor(data$x,data$y, method = "pearson")
xydata <- data.frame(x_mean, y_mean, x_sd, y_sd, corr)
return(xydata)
}

# test function on data frame
df_results <- xy_stats(df)

This produces the output:

> xy_stats(df)
    x_mean   y_mean      x_sd      y_sd       corr
1 54.73075 47.38462 0.4017586 0.2905615 -0.2230826

Then I create the expected output:

# test function on known results
test_data <- c(
            "x_mean" = 54.26,
            "y_mean" = 47.83,
            "x_sd" = 0.46,
            "y_sd" = 0.29,
            "corr" = -0.265
         )

Which looks like:

> test_data
x_mean y_mean   x_sd   y_sd   corr 
54.260 47.830  0.460  0.290 -0.265

I then compare the function output and the expected output:

library(testthat)
expect_equal(df_results,test_data,tolerance=1)

The output is as follows:

Error: `df_results` not equal to `test_data`.
Modes: list, numeric
Attributes: < names for target but not for current >
Attributes: < Length mismatch: comparison on first 0 components >

I cannot adjust the expected (test_data) results, but I can adjust the function to create an output that matches the expected results. I can see that the class of the test_data is numeric and the class of the df results is data.frame, but I don't know how to get the results produced by the function to be numeric. I have tried replacing the following in the code but it does not work:

# Replace:
xydata <- data.frame(x_mean, y_mean, x_sd, y_sd, corr)
# with:
xydata <- data.frame(as.numeric(x_mean, y_mean, x_sd, y_sd, corr))

Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>