我有一个如下所示的数据框:
for (N in seq(100,500,50)) {
P <- rep(NA,1000)
for (i in 1:1000) {
P[i] <- function()
}
result <- data.frame(P) # this is simplified
}
我想将特定行的值(基于条件选择或类似的东西)复制到新列。我想定位第二个单词的最后一个字母(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
中的值与Letter
中的值相同),在这些示例的情况下是这些行:
Word2
然后向数据框添加新列7 1 2 3 the nice apple e
18 6 7 8 yes did you d
,如下所示:
LastLetterWord2
是否可以在R中执行此操作,如果是,如何执行此操作? (我还不是R的专家)。一个可能的问题可能是单词不是唯一的,可能会多次出现并处于不同的位置(如Word1,Word2或Word3)。
答案 0 :(得分:0)
使用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
答案 1 :(得分:0)
使用stringr库中的str_sub也会这样做:
df$LastLetterWord2 =stringr::str_sub(df$Word2, start = -1, end = -1) # using minus to indicate from the end
答案 2 :(得分:0)
如果您的表名是DF
。
数据强>
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)
<强>命令强>
nchar
返回字符向量的大小。 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