
数据结构
|
157
#> a b c
#> 1 1 2 a
当然,如果你获得了数据并且它已经存在于具有因子的数据框中,你可以使用此方法将
所有因子转换为字符:
# same setup as in the previous examples
l1 <- list( a=1, b=2, c='X' )
l2 <- list( a=3, b=4, c='Y' )
l3 <- list( a=5, b=6, c='Z' )
obs <- list(l1, l2, l3)
df <- do.call(rbind, Map(as.data.frame, obs))
# Yes, you could use stringsAsFactors=FALSE above,
# but we're assuming the data.frame
# came to you with factors already
i <- sapply(df, is.factor) # determine which columns are factors
df[i] <- lapply(df[i], as.character) # turn only the factors to characters
请记住,如果使用 tibble 而不是数据框,则默认情况下不会强制字符成为因子。
5.19.4 另请参阅
如果数据按列而不是行组织,请参阅 5.18 节。
5.20 对数据框添加行
5.20.1 问题
需要将一个或多个新行添加到数据框。
5.20.2