topic_trend: Simple Rise or Fall Trend of Several Years

Description Usage Arguments Details Value Examples

View source: R/topic_trend.R

Description

When topic names and corresponding years are given, this function computes the rise and fall trend during the period by lm.

Usage

1
topic_trend(year, topic, relative = FALSE, zero = 0)

Arguments

year

a numeric vector of years for corresponding topics, if it is not numeric, the function will try to coerce. The years should be written in full-digit, that is, if they are 1998 and 2013, do not simply write 98 and 13. No NA is allowed. And, the number of unique years is at least 3, otherwise an error will be raised.

topic

a character vector of topics. If it is not character, the function will try to coerce. The length of topic and year should be the same. No NA is allowed.

relative

if FALSE (default), the numbers of topics is used. If TRUE, the percentage of a topic in a year against the total number of that year is used. Suppose this year we have 200 texts on art, and the total number of texts in this year is 1000, then the relative value is 200/1000 = 0.2 rather than the absolute number 200. Note: if to use relative value, NA of the amount of a topic will be automatically set to 0.

zero

this can only be 0 (default) or NA. Suppose we have 0 text on a certain topic, then you will make sure whether the amount is really 0, or the data of this topic in that year is missing. Set this argument to NA to make all 0 into NA.

Details

The detail of trend info in the result is as follows:

When computing trend for a topic, if less than 3 years has valid value and value in other years are all NA, then trendIndex, trendLevel and maxminYear will be -999, and other cells are "less than 3y". If the numbers of a topic do not change through years, then trendIndex will be 0, trendLevel and maxminYear will be -999, totalTrend and detailTrend will be "almost same".

Value

a list. The 1st element is trend info. The 2nd is a summary of amount of each topic in each year. If argument relative is TRUE, a 3rd element is returned, which is the relative value (percentage) of each topic in each year.

Examples

1
2
3
4
5
6
7
set.seed(1)
topic <- sample(c("art", "economy", "law", "politics", "sociology"), 50, replace = TRUE)
set.seed(2)
year <- sample(2011: 2016, 50, replace = TRUE)
tr1 <- topic_trend(year, topic)
tr2 <- topic_trend(year, topic, zero = NA)
tr3 <- topic_trend(year, topic, relative=TRUE)

chinese.misc documentation built on Sept. 13, 2020, 5:13 p.m.