如何根据其他列中的值将一列中的特定值向上移动一行?

时间:2019-07-16 17:32:55

标签: r

以下代码创建的数据框与我正在使用的数据框非常相似。

condition <- c("ITI", "pos","ITI", "ITI", "neg", "ITI", "ITI", "ITI", "pos", "ITI", "neg", "ITI", "ITI", "pos", "ITI")

response <- c("None", 6, "None", "None", "None", 7, "None", "None", 7, "None", "None", 6, "None", 6, "None")

rt <- c(NA, 1.5, NA, NA, NA, .2, NA, NA, 1.2, NA, NA, .4, NA, 1.4, NA)

d <- as.data.frame(cbind(condition, response, rt))

条件列在条件栏中,响应响应和反应时间以rt表示。我遇到的问题是,“ ITI”试验在“响应”或“ rt”列中不应包含任何内容,如果这样做,则意味着该受试者回答得较晚。我需要确定发生这种情况的位置并将这些值上移一行。我也需要在rt值上加上2。

我能够弄清楚如何使用逻辑索引来识别值,但是我无法弄清楚如何将它们移动到适当的位置。

d$response[d$condition == "ITI" & d$response != "None"] 

d$rt[d$condition == "ITI" & !is.na(d$rt)]

这就是数据框的外观。

condition <- c("ITI", "pos","ITI", "ITI", "neg", "ITI", "ITI", "ITI", "pos", "ITI", "neg", "ITI", "ITI", "pos", "ITI")

response <- c("None", 6, "None", "None", 7, "None", "None", "None", 7, "None", 6, "None", "None", 6, "None")

rt <- c(NA, 1.5, NA, NA, 2.2, NA, NA, NA, 1.2, NA, 2.4, NA, NA, 1.4, NA)

d <- as.data.frame(cbind(condition, response, rt))

4 个答案:

答案 0 :(得分:2)

尝试一下:

d$rt <- as.numeric(as.character(d$rt))
row_index <- which(d$condition == 'ITI' & d$response != 'None' & !is.na(d$rt))
new_d <- d
new_d [row_index-1, 'response'] <- d[row_index, 'response']
new_d [row_index-1, 'rt'] <- as.numeric(as.character(d[row_index, 'rt']))+2
new_d [row_index, 'response'] <- 'None'
new_d [row_index, 'rt'] <- NA

答案 1 :(得分:1)

可以尝试:

**C:\AzureLearn\**

输出:

library(dplyr)

d %>%
  mutate(
    rt = as.numeric(as.character(rt))
  ) %>%
  mutate_at(
    vars(response, rt),
    ~ case_when(
      lead(condition) == 'ITI' & !is.na(lead(rt)) ~ if (class(.) == 'factor') lead(.) else lead(.) + 2,
      condition == 'ITI' & !is.na(rt) ~ lag(.),
      TRUE ~ .
    )
  )

答案 2 :(得分:1)

这类似于Soodi的答案,但由于有data.table,因此语法略好(imo)

    <?php
        $rsmex = explode(",",$data['user']->statesId);
    ?>


        <lable for="addStorename1" >Market Name</label>
            <select class="form-control my-select"  data-live-search="true" data-actions-box="true"  placeholder="Enter Role" name="Statename[]" multiple  >    
        @foreach($rsmex as $rsmsl)
                @foreach($data['state'] as $rsms)
                    <option @if ( $rsmsl == $rsms->statesId ) {{"selected"}} @endif value={{$rsms->statesId}}>{{$rsms->statesName}}</option>
                @endforeach
        @endforeach 
         </select>

答案 3 :(得分:0)

这是一个有趣的任务!这就是我的处理方法。

首先,让我们暂时不要担心ITINone

cond.f <- condition[ condition != "ITI" ]
resp.f <- response[ response != "None" ]
rt.f <- rt[ !is.na(rt) ]

all(c(length(cond.f), length(resp.f)) == length(rt.f)) # TRUE

d.f <- data.frame(condition=cond.f, response=resp.f, rt=rt.f)

现在我们需要填写ITI

## indices of the ITI rows:
ITI <- d$condition == "ITI"

## nmap 
nmap <- rep(NA, nrow(d))
nmap[ !ITI ] <- 1:sum(!ITI)

好的,这有点令人费解,但是基本上nmap现在位于原始d中来自d.f的索引中的正确位置。在所有其他职位上都是不适用,所以我们现在可以这样做

d.f <- d.f[ nmap, ]

和d.f现在具有与d相同的行数,除了对应于ITI的行现在充满了NA。让我们摆脱NA:

d.f[ ITI, ] <- data.frame("ITI", "None", NA)
rownames(d.f) <- NULL

结果:

> d.f
   condition response  rt
1        ITI     None  NA
2        pos        6 1.5
3        ITI     None  NA
4        ITI     None  NA
5        neg        7 0.2
6        ITI     None  NA
7        ITI     None  NA
8        ITI     None  NA
9        pos        7 1.2
10       ITI     None  NA
11       neg        6 0.4
12       ITI     None  NA
13       ITI     None  NA
14       pos        6 1.4
15       ITI     None  NA