如果value是%vector中的%,如何替换列中的值

时间:2016-04-19 10:41:39

标签: r dataframe

我有两个向量,如:

one <- c("A", "B", "C", "D", "E", "F")
two <- c("1", "2", "3", "4", "5", "6")

然后我有一个包含原始值的数据框:

from   to 
A      1
2      A
3      B
F      B
C      4
D      5
6      6

我想替换原始df中的所有条目,如:

from   to
one    two
two    one
two    one
one    one
one    two
one    two
two    two

我正在尝试关注this,但它只适用于我做的第一次检查,然后转换所有数字:

df[] <- as.data.frame(lapply(df, function(x) ifelse(x%in%one, "one", x)))

当然这只是一个假的例子,在我的实际案例中,这里报告的时间太长,原始df中的所有值都是字符串

2 个答案:

答案 0 :(得分:3)

我们将'one','two'向量放在liststack中创建一个键,值data.frame('d1'),循环遍历初始数据集({ {1}})和lapply(df1,..带有'values'列的向量来获取'index'并替换为与索引匹配的'ind'列。

match

数据

d1 <- stack(list(one=one, two=two))
df2 <- df1
df2[] <- lapply(df1, function(x) d1$ind[match(x, d1$values)])
df2
#  from  to
#1  one two
#2  two one
#3  two one
#4  one one
#5  one two
#6  one two
#7  two two

答案 1 :(得分:1)

我认为这就是你要做的事情:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<html>
<body>
<ul  class="heads nav nav-tabs" role="tablist">
  <li class="col-md-3 col-sm-3 col-xs-3">
    <a class="odkaz kis" role="tab" data-toggle="tab" href="#collapseKisela" aria-expanded="false" aria-controls="#collapseKisela" data-parent='#accordeon'>
      <div class="team-members">
        <div class="team-avatar core-foto">
          <img data-alt-src="img/profile/Kisela_t.jpg" src="img/profile/Kisela_b.jpg"  class="xyz img-responsive" alt="">
        </div>
      </div>
    </a>	
  </li>
  <li class="col-md-3 col-sm-3 col-xs-3">
    <a class="odkaz kraus" role="tab" data-toggle="tab" href="#collapseKrausz" aria-expanded="false" aria-controls="#collapseKrausz" data-parent='#accordeon'>
      <div class="team-members">
        <div class="team-avatar core-foto">
          <img data-alt-src="img/profile/Krausz_t.jpg" src="img/profile/Krausz_b.jpg"  class="xyz img-responsive" alt="">
        </div>	
      </div>
    </a>	
  </li>
  <li class="col-md-3 col-sm-3 col-xs-3">
    <a class="odkaz" role="tab" data-toggle="tab" href="#collapseJancusko" aria-expanded="false" aria-controls="#collapseJancusko" data-parent='#accordeon'>
      <div class="team-members">
        <div class="team-avatar core-foto">
          <img data-alt-src="img/profile/Jancusko_t.jpg" src="img/profile/Jancusko_b.jpg"  class="xyz img-responsive" alt="">
        </div>	
      </div>
    </a>	
  </li>
  <li class="col-md-3 col-sm-3 col-xs-3">
    <a class="odkaz" role="tab" data-toggle="tab" href="#collapseBerka" aria-expanded="false" aria-controls="#collapseKisela" data-parent='#accordeon'>
      <div class="team-members">
        <div class="team-avatar core-foto">
          <img data-alt-src="img/profile/Berka_t.jpg" src="img/profile/Berka_b.jpg" name="" role="presentation" class="active img-responsive" alt="">
        </div>	
        </a>	
      </li>
  </ul>
		
  <div class="row tab-content">
    <div role="tabpanel" class="tab-pane fade" id="collapseBerka">
      <div class="hlavy">
        <h4>René Krausz</h4>
        <h5>CEO</h5>
        <p>Some text here</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vero atque voluptates maiores magnam expedita inventore eaque alias mollitia voluptate corporis harum ut quod quaerat dolorem, illo fugit soluta impedit, maxime.</p>
      </div>
    </div>
    <div role="tabpanel" class="tab-pane fade" id="collapseKrausz">
      <div class="hlavy">
        <h4>René Krausz</h4>
        <h5>CEO</h5>
        <p>Some text herer</p>
        <p>Text</p>
      </div>
    </div>
    <div role="tabpanel" class="tab-pane fade" id="collapseJancusko">
      <div class="hlavy">
        <h4>Lukáš Jancusko</h4>
        <h5>CEO</h5>
        <p>Some text here</p>
        <p>Changing text</p>
      </div>
    </div>

    <div role="tabpanel" class="tab-pane fade" id="collapseGrega">
      <div class="hlavy">
        <h4>Tomáš Grega</h4>
        <h5>CEO</h5>
        <p>Some text here</p>
        <p>Another text</p>
      </div>
    </div>
    <div role="tabpanel" class="tab-pane fade" id="collapseKisela">
      <div class="hlavy">
        <h4>Juraj Kisela</h4>
        <h5>CEO</h5>
        <p>Some text here</p>
        <p>TEXT</p>
      </div>
    </div>
</body>
</html>

或类似地,使用> df <- data.frame(from=c("A",2,3,"F","C","D",6),to=c(1,"A","B","B",4:6)) > one <- c("A", "B", "C", "D", "E", "F") > two <- c("1", "2", "3", "4", "5", "6") > as.data.frame(apply(df,2,function(x)ifelse(x%in%one,"one",ifelse(x%in%two,"two",x)))) from to 1 one two 2 two one 3 two one 4 one one 5 one two 6 one two 7 two two 技巧:

df[]

或者再次使用> df[]<-apply(df,2,function(x)ifelse(x%in%one,"one",ifelse(x%in%two,"two",x))) > df from to 1 one two 2 two one 3 two one 4 one one 5 one two 6 one two 7 two two

lapply