简体   繁体   English

R:将值从特定行(属于组的一部分)复制到列

[英]R: Copy a value from a specific row (that is part of a group) to a column

I have a dataframe that looks like this: 我有一个看起来像这样的数据框:

RowID   ID1     ID2     ID3     Word1       Word2       Word3       Letter  
1       1       2       3       the         nice        apple       t
2       1       2       3       the         nice        apple       h
3       1       2       3       the         nice        apple       e
4       1       2       3       the         nice        apple       n
5       1       2       3       the         nice        apple       i
6       1       2       3       the         nice        apple       c
7       1       2       3       the         nice        apple       e
8       1       2       3       the         nice        apple       a
9       1       2       3       the         nice        apple       p
10      1       2       3       the         nice        apple       p
11      1       2       3       the         nice        apple       l
12      1       2       3       the         nice        apple       e
13      6       7       8       yes         did         you         y
14      6       7       8       yes         did         you         e
15      6       7       8       yes         did         you         s
16      6       7       8       yes         did         you         d
17      6       7       8       yes         did         you         i
18      6       7       8       yes         did         you         d
19      6       7       8       yes         did         you         y
20      6       7       8       yes         did         you         o
21      6       7       8       yes         did         you         u

I would like to copy values of from specific rows (based on a conditional select or something similar) to a new column. 我想将特定行(基于条件选择或类似条件)中的值复制到新列中。 I want to target the last letter of the second word (value in Letter as in Word2 ), which in the case of these examples are these rows: 我想定位第二个单词的最后一个字母(在Word2中为Letter中的Word2 ),在这些示例的情况下,这些行是这些行:

7       1       2       3       the         nice        apple       e
18      6       7       8       yes         did         you         d

And then add a new column LastLetterWord2 to the dataframe, like this: 然后将新列LastLetterWord2添加到数据LastLetterWord2 ,如下所示:

RowID   ID1     ID2     ID3     Word1       Word2       Word3       Letter  LastLetterWord2
1       1       2       3       the         nice        apple       t       e   
2       1       2       3       the         nice        apple       h       e
3       1       2       3       the         nice        apple       e       e
4       1       2       3       the         nice        apple       n       e
5       1       2       3       the         nice        apple       i       e
6       1       2       3       the         nice        apple       c       e
7       1       2       3       the         nice        apple       e       e
8       1       2       3       the         nice        apple       a       e
9       1       2       3       the         nice        apple       p       e
10      1       2       3       the         nice        apple       p       e
11      1       2       3       the         nice        apple       l       e
12      1       2       3       the         nice        apple       e       e
13      6       7       8       yes         did         you         y       d
14      6       7       8       yes         did         you         e       d
15      6       7       8       yes         did         you         s       d
16      6       7       8       yes         did         you         d       d
17      6       7       8       yes         did         you         i       d
18      6       7       8       yes         did         you         d       d
19      6       7       8       yes         did         you         y       d
20      6       7       8       yes         did         you         o       d
21      6       7       8       yes         did         you         u       d

Is it possible to do this in R, and if so, how? 可以在R中执行此操作吗?如果可以,如何执行? (I'm not much of an expert in R yet). (我还不是R方面的专家)。 One possible problem might be that words are not unique and can occur many times and in different positions (so as Word1, Word2 or Word3). 一个可能的问题可能是单词不是唯一的,并且可能出现多次并且出现在不同的位置(例如Word1,Word2或Word3)。

With substr and nchar you can easily create a new column with the last letter of Word2 column : 使用substrnchar您可以轻松地用Word2列的最后一个字母创建一个新列:

DF$LastLetterWord2 <- substr(DF$Word2,nchar(DF$Word2),nchar(DF$Word2))

> DF
   RowID ID1 ID2 ID3 Word1 Word2 Word3 Letter LastLetterWord2
1      1   1   2   3   the  nice apple      t               e
2      2   1   2   3   the  nice apple      h               e
3      3   1   2   3   the  nice apple      e               e
4      4   1   2   3   the  nice apple      n               e
5      5   1   2   3   the  nice apple      i               e
6      6   1   2   3   the  nice apple      c               e
7      7   1   2   3   the  nice apple      e               e
8      8   1   2   3   the  nice apple      a               e
9      9   1   2   3   the  nice apple      p               e
10    10   1   2   3   the  nice apple      p               e
11    11   1   2   3   the  nice apple      l               e
12    12   1   2   3   the  nice apple      e               e
13    13   6   7   8   yes   did   you      y               d
14    14   6   7   8   yes   did   you      e               d
15    15   6   7   8   yes   did   you      s               d
16    16   6   7   8   yes   did   you      d               d
17    17   6   7   8   yes   did   you      i               d
18    18   6   7   8   yes   did   you      d               d
19    19   6   7   8   yes   did   you      y               d
20    20   6   7   8   yes   did   you      o               d
21    21   6   7   8   yes   did   you      u               d

使用stringr库中的str_sub也会执行相同的操作:

df$LastLetterWord2 =stringr::str_sub(df$Word2, start = -1, end = -1) # using minus to indicate from the end

If the name of your table is DF . 如果表名是DF

DATA 数据

RowID  <- 1:21
ID1    <- c(rep(1,12),rep(6,9))
ID2    <- c(rep(2,12),rep(7,9))
ID3    <- c(rep(3,12),rep(8,9))
Word1  <- c(rep("the",12),rep("yes",9))
Word2  <- c(rep("nice",12),rep("did",9))
Word3  <- c(rep("apple",12),rep("you",9))
Letter <- c("t","h","e","n","i","c","e","a","p","p","l","e","y","e","s","d","i","d","y","o","u")
DF     <- data.frame(RowID,ID1,ID2,ID3,Word1,Word2,Word3,Letter)

Command 命令

nchar returns the size of character vector. nchar返回字符向量的大小。 substring extracts letters from character vector. substring从字符向量中提取字母。

numberofcharacter  <- data.frame(apply(DF,2,nchar))
DF$LastLetterWord2 <- substring(DF$Word2,numberofcharacter$Word2,numberofcharacter$Word2)

RESULT 结果

   RowID ID1 ID2 ID3 Word1 Word2 Word3 Letter LastLetterWord2
1      1   1   2   3   the  nice apple      t               e
2      2   1   2   3   the  nice apple      h               e
3      3   1   2   3   the  nice apple      e               e
4      4   1   2   3   the  nice apple      n               e
5      5   1   2   3   the  nice apple      i               e
6      6   1   2   3   the  nice apple      c               e
7      7   1   2   3   the  nice apple      e               e
8      8   1   2   3   the  nice apple      a               e
9      9   1   2   3   the  nice apple      p               e
10    10   1   2   3   the  nice apple      p               e
11    11   1   2   3   the  nice apple      l               e
12    12   1   2   3   the  nice apple      e               e
13    13   6   7   8   yes   did   you      y               d
14    14   6   7   8   yes   did   you      e               d
15    15   6   7   8   yes   did   you      s               d
16    16   6   7   8   yes   did   you      d               d
17    17   6   7   8   yes   did   you      i               d
18    18   6   7   8   yes   did   you      d               d
19    19   6   7   8   yes   did   you      y               d
20    20   6   7   8   yes   did   you      o               d
21    21   6   7   8   yes   did   you      u               d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据条件 r 在行上复制特定值 - copy specific value on a row based on conditions r 从 R dataframe 中的特定行将数据从一列复制到另一列 - Copy data from one column to another column from a specific row in R dataframe 我想过滤组 id 在 r 中的列和某些行值上满足的特定条件 - I want to filter group id's specific conditions meeting on both column and some row value in r 将值分配给R中的特定列和行 - Assigning value to a specific column and row in R 根据列值分组,然后将该组作为一行添加到 r 中的 dataframe - Group by based on a column value and then add the group as a row to a dataframe in r R:根据组与另一列的最大值的行差异的新列 - R: new column of row difference from max value of another column according to group R 按组为特定日期行在另一列中查找上个月 - R by group for a specific date row find the preceding month in another column 在 R 中查询组的特定行以创建新列 - Querying an specific row of a group in R to create a new column 使用 R 根据特定列中至少出现一次的特定值创建一个新列? - Create a new column based on a specific value appearing in a row at least once from the specific columns using R? 按组聚合时从特定行粘贴data.table(R)值 - data.table (R) paste value from a specific row while aggregating by group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM