df <- data.frame(name = c("黄芪当归汤", "黄芪汤", "当归汤", "人参当归汤"), stringsAsFactors = FALSE) df[grep("当归.*黄芪", df$name), ]
时间: 2023-12-11 14:33:15 浏览: 165
这段代码的作用是从数据框df中选取名称中包含“当归”和“黄芪”的行。其中,grep()函数用于在字符串中查找匹配项,返回匹配项的位置,而[]用于选取行。具体来说,grep("当归.*黄芪", df$name)返回名称中包含“当归”和“黄芪”的行的位置,然后将这些位置作为下标传递给df[],即可选取相应的行。
相关问题
A <- data.frame(v=character(),m=numeric()) for(i in colnames(df)){missing<-sum(df[[i]]=="") append(A$v,i) append(A$m,missiong)},运行完后 为什么A是个空的数据框
根据你提供的代码,A是一个空数据框,因为在第一行中定义了A为一个没有任何行和列的数据框,即使在for循环中使用append()函数向A中添加数据,也不会改变A的结构。如果你希望A成为一个与df相同列名的数据框,可以使用以下代码:
```R
A <- data.frame(v=colnames(df), m=rep(0, ncol(df)))
for(i in colnames(df)){
missing <- sum(df[[i]]=="")
A[m == i, "m"] <- missing
}
```
这里使用rep()函数来创建与df相同列数的0向量,并在for循环中使用A[m == i, "m"] <- missing来将每个列的缺失值数量添加到A中。
df0 <- data.frame( date = rep(res.seir$days,2), yy = c(res.seir$DI,res.seir$rate), type = rep(c("(B)每日新增人数","(A)累计感染率"),each=55))
好的,你的第三个问题是如何创建一个名为 df0 的数据框?正确的代码是
```
df0 <- data.frame(
date = rep(res.seir$days, 2),
yy = c(res.seir$DI, res.seir$rate),
type = rep(c("(B)每日新增人数", "(A)累计感染率"), each = 55)
)
```
该代码将会创建一个数据框,其中包含三个列:date、yy 和 type。date 列是 res.seir$days 的重复值,yy 列是 res.seir$DI 和 res.seir$rate 的组合,type 列是两个字符串值的重复值。其中 "(B)每日新增人数" 重复了 55 次,"(A)累计感染率" 也重复了 55 次。
阅读全文
相关推荐
















