R从数据框中的列和行中提取值

时间:2019-03-21 08:17:09

标签: r dataframe data-extraction

我有这样的数据框

#dt
#   one two row MAX_row three four
#1: a   1   0   2       yes   yes
#2: a   2   2   2       yes   yes
#3: a   3   0   2       no    yes
#4: b   1   0   5       yes   no
#5: b   2   5   5       no    no
#6: b   3   0   5       no    no

要创建变量行和MAX_row,我产生的代码如下:

dt$row <-ifelse(dt$two == 2,rownames(dt), 0)
dt <- dt %>% group_by(one) %>% mutate(MAX_row=max(row))

现在我要尝试的是用第三列中的值填充第四列。行号在列MAX_row中指示。因此,在第四列中,第一列中带有“ a”的行应该是第三列第二行中的值,就像我在dt中显示的那样。 我认为下面的代码可以,但是会产生奇数值:

dt$four <- ifelse(dt$one=='a',dt$three[dt$MAX_row],0)

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

如果我对它的理解正确,请从三列string datetime = DateTime.Now.ToString("yyyy-MM-dd-HHMM"); string filePath = $@"C:\folder\Text{datetime}.txt"; File.Create(filePath); TextWriter writer = new StreamWriter(path); for(int i = 0; i < dataGridView1.Rows.Count-1; i++) { for(int j = 0; j < dataGridView1.Columns.Count; j++) { writer.Write("\t"+dataGridView1.Rows[i].Cells[j].Value.ToString()+"\t"+"|"); } writer.WriteLine(""); writer.WriteLine("-----------------------------------------------------"); } writer.Close(); MessageBox.Show("Data Exported"); onetwo开始,我认为threerow是为到达MAX_row

我们无需创建这些变量即可获得预期的输出。

four

这仍然可以提供预期的输出,而无需创建library(dplyr) df %>% group_by(one) %>% mutate(four = three[which.max(two == 2)]) # one two three four # <fct> <int> <fct> <fct> #1 a 1 yes yes #2 a 2 yes yes #3 a 3 no yes #4 b 1 yes no #5 b 2 no no #6 b 3 no no row

数据

MAX_row

答案 1 :(得分:0)

最好不要混用data.tabledplyr语法。由于dt似乎是data.table,因此这里是data.table解决方案

dt[
    , row := ifelse(two == 2, .I, 0)][,
    , MAX_row := max(row), by = one][,
    , four := ifelse(one == "a", three[MAX_row], 0)]
#   one two row MAX_row three four
#1:   a   1   0       2   yes  yes
#2:   a   2   2       2   yes  yes
#3:   a   3   0       2    no  yes
#4:   b   1   0       5   yes   no
#5:   b   2   5       5    no   no
#6:   b   3   0       5    no   no

或者全部避免生成rowMAX_row(如Ronak所强调的那样)

dt[, four := three[which.max(two == 2)], by = one]
#   one two row MAX_row three four
#1:   a   1   0       2   yes  yes
#2:   a   2   2       2   yes  yes
#3:   a   3   0       2    no  yes
#4:   b   1   0       5   yes   no
#5:   b   2   5       5    no   no
#6:   b   3   0       5    no   no