Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用相等索引替换数字元素_R_Indexing - Fatal编程技术网

R 用相等索引替换数字元素

R 用相等索引替换数字元素,r,indexing,R,Indexing,我有一个大数据帧尺寸(13265x40)。数据帧是通过以下方式将多个单独的数据帧组合在一个列表中创建的: big\u dataframe=bind\u行(dataframe\u列表,.id=“Index”)。 .id=“Index”为我提供了一个名为“Index”的附加列,带有数字输入(1、2、3、4等),因此我仍然可以识别大数据帧中的独立数据帧 例如: big_dataframe: Index A B C D 1 3 6 7 1 1 6 6 2 0 1

我有一个大数据帧
尺寸(13265x40)
。数据帧是通过以下方式将多个单独的数据帧组合在一个列表中创建的:
big\u dataframe=bind\u行(dataframe\u列表,.id=“Index”)
.id=“Index”
为我提供了一个名为
“Index”
的附加列,带有数字输入(1、2、3、4等),因此我仍然可以识别
大数据帧中的独立数据帧

例如:

big_dataframe:
Index  A  B  C  D
1      3  6  7  1
1      6  6  2  0
1      7  5  4  2
2      4  0  7  4
2      3  2  9  1
2      9  4  4  5
3      7  1  5  2
3      3  9  3  4
3      1  1  2  10
4      10 6  7  1
等等。 然后我有一个索引列表:
indexlist=c(“John”、“Peter”、“Michael”、“Brian”等)
,它包含
“character”
元素

目标是用相等的索引替换
big_dataframe$Index
中的数值输入。 例如:

我对此有点迷茫,不知道如何解决这个问题


提前感谢

如果“索引”是数字,并且从1开始,它可以用作索引,以相同的顺序将值替换为“indexlist”值

big_dataframe$Index <- indexlist[big_dataframe$Index]

big\u dataframe$Index这实际上为
big\u dataframe$Index
NA
@Mcgroger这个案例很重要
big\u dataframe$Index
,我之前用过
big\u dataframe$Index
,你现在可以检查一下吗。如果这不起作用,您能否检查
索引
列是否为
数值
类(dfl\u组合$Index)=“character”
,因此可能是问题所在。@Mcgroger在这种情况下,只需根据输入数据集更改为
indexlist[as.integer(big\u dataframe$Index)]
,为什么“索引”中的3被“彼得”取代。那应该是‘迈克尔’吗
big_dataframe$Index <- indexlist[big_dataframe$Index]
big_dataframe <- structure(list(Index = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
4L), A = c(3L, 6L, 7L, 4L, 3L, 9L, 7L, 3L, 1L, 10L), B = c(6L, 
6L, 5L, 0L, 2L, 4L, 1L, 9L, 1L, 6L), C = c(7L, 2L, 4L, 7L, 9L, 
4L, 5L, 3L, 2L, 7L), D = c(1L, 0L, 2L, 4L, 1L, 5L, 2L, 4L, 10L, 
1L)), class = "data.frame", row.names = c(NA, -10L))

indexlist <- c("John", "Peter", "Michael", "Brian")