Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何根据另一列上的选择条件从列中选择值?_R - Fatal编程技术网

R 如何根据另一列上的选择条件从列中选择值?

R 如何根据另一列上的选择条件从列中选择值?,r,R,如何根据R中另一列中的值的频率(重复次数)从列中选择值/名称 这就是我的示例数据..: Col1 col2 Col3 Col4 Yuva 123 qwe XXYY Arun 234 asd XYXY Yuva 870 ghj XXYX Naan 890 qwe XYYX Shan 231 asd YXYX Yuva 453 qwe YYXY Naan 314 ghj YXYY Yuva

如何根据R中另一列中的值的频率(重复次数)从列中选择值/名称

这就是我的示例数据..:

 Col1   col2  Col3  Col4 
 Yuva    123  qwe   XXYY
 Arun    234  asd   XYXY
 Yuva    870  ghj   XXYX
 Naan    890  qwe   XYYX
 Shan    231  asd   YXYX
 Yuva    453  qwe   YYXY
 Naan    314  ghj   YXYY
 Yuva    908  bnm   YYYx
现在我想知道函数/语句,它根据Col3中的值出现的次数,从col1和Col3中给我一对值。i、 例如,当'qwe'发生一次(/两次/三次)时,col1中的对应值是多少。 预期(必需)答案是:

                  Upon giving   I should get
                 --------------------------- 
                    qwe =1      Naan
                    qwe =2      Yuva 
                    qwe=3       ------(not available). similarly
                    asd=1       Arun
                                Shan
                    ghj=1       Yuva
                                Naan
                    ghj = 2      -----(not available)
           and for  bnm=1       Yuva.

伙计们,请帮帮我。

也许是这样的:

x <- read.table(textConnection("Col1\tcol2\tCol3\tCol4\nYuva\t123\tqwe\tXXYY\nArun\t234\tasd\tXYXY\nYuva\t870\tghj\tXXYX\nNaan\t890\tqwe\tXYYX\nShan\t231\tasd\tYXYX\nYuva\t453\tqwe\tYYXY\nNaan\t314\tghj\tYXYY\nYuva\t908\tbnm\tYYYx\n"), header=TRUE, sep="\t", stringsAsFactors=FALSE)

selCol1 <- function(x, valCol3, occur) {
    s <- subset(x, Col3==valCol3)
    f <- as.factor(s$Col1)
    t <- table(f)
    idx <- which(t==occur)
    if(length(idx)==0)
        return(NA)
    else
        return(levels(f)[idx])
}

selCol1(x,"qwe",1)
selCol1(x,"qwe",2)
selCol1(x,"qwe",3)
selCol1(x,"ghj",1)
selCol1(x,"ghj",2)
selCol1(x,"bnm",1)

xxtabs函数返回支持矩阵索引的列联表:

getCombs <- function(nam , cnt) names( 
                   which(xtabs( ~Col1+Col3, data=dat)[ ,nam] == cnt)
                                    )

> getCombs("ghj", 1)
[1] "Naan" "Yuva"
> getCombs("ghj", 3)
character(0)
getCombs getCombs(“ghj”,1)
[1] “纳安”“尤瓦”
>getCombs(“ghj”,3)
字符(0)

如果需要将“notavailable”作为值,则只需测试length()==0的结果,如果需要,则返回该字符串。

尝试以下代码
plyr::ddply(df,((Col3,Col1),transform,num=length(col2))
。要做到这一点,你需要安装
plyr
软件包。我读了你的段落“现在我想……答案是:”至少三次,但我仍然无法理解你想说什么。。。