[英]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 : 使用
substr
和nchar
您可以轻松地用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.