WASHINGTON — The number of violent attacks and incidents of bullying in American public schools has gone down in recent years, according to a federal report published Thursday.

Violence and bullying were more frequent in middle schools than in high schools or elementary schools, said the study by the Education Department’s National Center for Education Statistics.

The report said the rate of violent incidents in middle schools dropped from 40 incidents per 1,000 students in the 2009-2010 school year to 27 incidents in 2015-2016. Bullying in middle schools was observed in 39 percent of schools in 2009-2010, compared to 22 percent last school year.

In 2009-2010, when the previous such survey was conducted, about 46 percent of schools reported that their students were threatened without a weapon and 8 percent with a weapon, compared to 39 percent and 9 percent respectively during the last school year.

Substance abuse stayed unchanged. Last school year, 25 percent of schools reported at least one drug-related incident, same as in the previous study.

The survey was based on a random sample of some 3,500 schools. It did not provide an explanation for the trends.