Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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 - Fatal编程技术网

R 根据列编号替换列中的值

R 根据列编号替换列中的值,r,R,在以下脚本中: dataset <- read.csv("/home/adam/Desktop/Temp/lrtest.csv") for(i in 3:ncol(dataset)){ uq <- unique(dataset[,i]) j <- i * 100 for(x in uq){ dataset[,i][dataset[,i] == x] <- j #dataset$nm[dataset$nm == x] <-

在以下脚本中:

  dataset <- read.csv("/home/adam/Desktop/Temp/lrtest.csv")
  for(i in 3:ncol(dataset)){
    uq <- unique(dataset[,i])
    j <- i * 100
    for(x in uq){
      dataset[,i][dataset[,i] == x] <- j #dataset$nm[dataset$nm == x] <- j
      j <- j + 1
    }
  }
输出:

      Class Branch LA_type Method_type Method_call Branch_type Branch_condition Tested_parameter
1      Goal     12    <NA>        <NA>        <NA>        <NA>              700             <NA>
2   TreeApp     20    <NA>        <NA>        <NA>        <NA>              701             <NA>
3  Password      4    <NA>        <NA>        <NA>        <NA>              701             <NA>
4 XMLParser      9    <NA>        <NA>        <NA>        <NA>              700             <NA>
5  MapClass     33    <NA>        <NA>        <NA>        <NA>              701             <NA>
Class Branch LA_type Method_type Method_call Branch_type Branch_condition Tested_参数
1目标12 700
2 TreeApp 20 701
3密码4 701
4 xml9 700
5地图类别33 701

我们可以使用
lappy
迭代第3列到数据帧的末尾,将数据转换为具有唯一级别的
因子(可能已经是),并增加100的递增顺序

df[3:ncol(df)] <- lapply(3:ncol(df), function(x) 
       x * 100 + as.integer(factor(df[[x]], levels = unique(df[[x]]))) - 1)

df
#      Class Branch LA_type Method_type Method_call Branch_type Branch_condition
#1      Goal     12     300         400         500         600              700
#2   TreeApp     20     301         401         501         601              701
#3  Password      4     300         402         500         602              701
#4 XMLParser      9     301         401         501         603              700
#5  MapClass     33     300         403         500         604              701

#  Tested_parameter
#1              800
#2              801
#3              801
#4              800
#5              802

df[3:ncol(df)]您期望的输出是什么?@RonakShah查看列
Branch\u条件
,因为它对它起作用,但对其他列不起作用。那么列3的值会是300、301、300、301和300吗?@RonakShah是的,应该是这样的,您只是在寻找
循环解决方案吗?如果您通过执行
df[]将列转换为字符,则代码将正常工作
df[3:ncol(df)] <- lapply(3:ncol(df), function(x) 
       x * 100 + as.integer(factor(df[[x]], levels = unique(df[[x]]))) - 1)

df
#      Class Branch LA_type Method_type Method_call Branch_type Branch_condition
#1      Goal     12     300         400         500         600              700
#2   TreeApp     20     301         401         501         601              701
#3  Password      4     300         402         500         602              701
#4 XMLParser      9     301         401         501         603              700
#5  MapClass     33     300         403         500         604              701

#  Tested_parameter
#1              800
#2              801
#3              801
#4              800
#5              802
df <- structure(list(Class = structure(c(1L, 4L, 3L, 5L, 2L), .Label = c("Goal", 
"MapClass", "Password", "TreeApp", "XMLParser"), class = "factor"), 
Branch = c(12L, 20L, 4L, 9L, 33L), LA_type = structure(c(2L, 
1L, 2L, 1L, 2L), .Label = c("Rugged", "Smooth"), class = "factor"), 
Method_type = structure(c(4L, 1L, 2L, 1L, 3L), .Label = c("constructor", 
"private", "public", "public_static"), class = "factor"), 
Method_call = structure(c(1L, 2L, 1L, 2L, 1L), .Label = c("never_called", 
"none"), class = "factor"), Branch_type = structure(c(4L, 
1L, 2L, 5L, 3L), .Label = c("IF_ICMPGE", "IFEQ", "IFGT", 
"IFNE", "IFNONNULL"), class = "factor"), Branch_condition = c(TRUE, 
FALSE, FALSE, TRUE, FALSE), Tested_parameter = structure(c(3L, 
2L, 2L, 3L, 1L), .Label = c("double", "int", "String"), class = "factor")), 
class = "data.frame", row.names = c(NA, -5L))