我无法让我的数据框添加额外的列。我已经检查了这么多堆栈溢出,但这里是一个子集(Adding a new column in a matrix in R,adding new column to data frame in R,new column not added to dataframe in R,R: complete a dataset with a new column added,R: add a new column to dataframes from a function)
我需要一个单列,告诉我们,如果有一个积极的或"1"在任何病毒行我有。
我试图确定概率和从我所看到的,我将需要这个列做进一步的计算,所以请帮助,如果可以!
样本数据
Filovirus (MOD) PCR : Phlebo (Sanchez-Seco) PCR
0 0
0 1
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
species code forest site
<fctr> <dbl> <fctr>
SM 1 UMNP-mangabey
SM 1 UMNP-mangabey
RC 9 UMNP-hondohondoc
BWC 9 UMNP-hondohondod
BWC 9 UMNP-hondohondod
BWC 9 UMNP-hondohondod
BWC 9 UMNP-hondohondod
BWC 9 UMNP-hondohondod
BWC 9 UMNP-hondohondod
BWC 9 UMNP-hondohondod
我得到的最接近的方法是用基数R来调用哪些行具有正值
我遵循了here的解决方案,但还没有让它为我工作。
tmp=which(data==1,arr.ind=T)
tmp=tmp[order(tmp[,"row"]),]
c("positive","negative")[tmp[,"col"]] -> data$new
如有任何建议,不胜感激。
数据输出
structure(list(`Filovirus (MOD) PCR` = c("0", "0", "0", "0",
"0", "0", "0", "0", "0", "0"), `Filovirus (A) PCR` = c("0", "0",
"0", "0", "0", "0", "0", "0", "0", "0"), `Filovirus (B) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Filo C PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Filovirus (D) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Coronavirus (Quan) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Coronavirus (Watanabe) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Paramyxo (Tong) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Flavivirus Moureau PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Flavivirus Sanchez-seco PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Arena Lozano 1 PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Retrovirus Courgnard PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Simian Foamy Goldberg (Pol) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Simian Foamy Goldberg (LTR Region) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Influenza (Anthony) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Influenza (Liang) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Rhabdo (CII) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Enterovirus CII I PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Enterovirus CII-II PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Alphav (Sanchez-Seco) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Lyssavirus (Vasquez-Moron) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Seadornavirus (CII) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Hantavirus (Raboni) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Hantavirus (Klempa) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Nipah (Wacharapleusadee) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Henipa (Feldman) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Bunya S (Briese) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Bunya L (Briese) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `Phlebo (Sanchez-Seco) PCR` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), species = structure(c(3L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("SM", "SY", "BWC",
"YB", "RC"), class = "factor"), code = c(2, 5, 5, 5, 5, 5, 5,
5, 5, 5), forestsite = structure(c(3L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L), .Label = c("Magombera1", "Magombera2", "NDUFR",
"Ndundulu1", "Ndundulu2", "Ndundulu3", "Nyumbanitu", "UMNP-campsite3",
"UMNP-hondohondoa", "UMNP-hondohondob", "UMNP-hondohondoc", "UMNP-hondohondod",
"UMNP-hondohondoe", "UMNP-HQ", "MamaGoti", "UMNP-mangabey", "UMNP-njokamoni",
"UMNP-Sanje1", "UMNP-Sanje2", "UMNP-Sanje3", "Sonjo", "SonjoRoad"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
3条答案
按热度按时间ovfsdjhp1#
0
和1
是字符类型。使用type.convert(as.is = TRUE)
转换为数字将使代码工作:dplyr
吊坠为:数据摘自@langtang(非常感谢):一个二个一个一个
hpxqektj2#
更新了给定字符列和新的32列示例
原始答案(假设为数字列):
您可以简单地执行以下操作:
输出:
输入:
xesrikrc3#
另一个选项是
if_any