Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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:如何在data.frame中使用apply循环变量_R_Loops_Dataframe_Apply_Sapply - Fatal编程技术网

R:如何在data.frame中使用apply循环变量

R:如何在data.frame中使用apply循环变量,r,loops,dataframe,apply,sapply,R,Loops,Dataframe,Apply,Sapply,我正在尝试学习如何使用apply(或apply家族的任何其他成员)在data.frame中循环变量 df_long <- data.frame(id=c(1,1,1,1,2,2,2,2,3,3,3,3), country=c('a','a','a','a','b','b','b','b','c','c','c','c'), year=c(1,2,3,4,1,2,3,4,1,2,3,4), amt = c

我正在尝试学习如何使用apply(或apply家族的任何其他成员)在data.frame中循环变量

    df_long <- data.frame(id=c(1,1,1,1,2,2,2,2,3,3,3,3), 
             country=c('a','a','a','a','b','b','b','b','c','c','c','c'),
             year=c(1,2,3,4,1,2,3,4,1,2,3,4),
             amt = c(3,4,23,5,76,5,2,3,5,4,6,2))
例如:假设我有以下data.frame

    df_long <- data.frame(id=c(1,1,1,1,2,2,2,2,3,3,3,3), 
             country=c('a','a','a','a','b','b','b','b','c','c','c','c'),
             year=c(1,2,3,4,1,2,3,4,1,2,3,4),
             amt = c(3,4,23,5,76,5,2,3,5,4,6,2))

对于这个问题或如何使用apply和/或其他方法在data.frame中循环变量的任何见解,我都将不胜感激。

我将首先使用is.code>is.numeric查找数值列,然后仅在这些列中添加1
sapply/lappy
在每列上循环,如果列是数字或不是数字,则返回TRUE/FALSE。我们使用该逻辑索引(
col\u ind
)对数据帧进行子集划分并向其添加1

col_ind <- sapply(df_long, is.numeric)
df_long[col_ind] <- df_long[col_ind] + 1
df_long

#   id country year amt
#1   2       a    2   4
#2   2       a    3   5
#3   2       a    4  24
#4   2       a    5   6
#5   3       b    2  77
#6   3       b    3   6
#7   3       b    4   3
#8   3       b    5   4
#9   4       c    2   6
#10  4       c    3   5
#11  4       c    4   7
#12  4       c    5   3

我尝试了使用
sapply
apply
来遵循您最初要求的方法,但挑战在于它试图将结果强制转换为矩阵。它要么强制将所有变量作为字符返回,要么将
国家
变量转换为数字,并将
a
转换为
1
b
转换为
2
,依此类推

如果您喜欢使用
apply
函数之一的单行代码,那么我建议使用
lappy
lappy
将以列表的形式返回结果,然后可以将其转换为数据帧。解决方案如下:

as.data.frame(
  lapply(
    df_long, 
    function(col) 
      if(is.numeric(col)) {col + 1} else {col}))
结果是:

   id country year amt
1   2       a    2   4
2   2       a    3   5
3   2       a    4  24
4   2       a    5   6
5   3       b    2  77
6   3       b    3   6
7   3       b    4   3
8   3       b    5   4
9   4       c    2   6
10  4       c    3   5
11  4       c    4   7
12  4       c    5   3

这有助于理解如何将函数应用于特定列
   id country year amt
1   2       a    2   4
2   2       a    3   5
3   2       a    4  24
4   2       a    5   6
5   3       b    2  77
6   3       b    3   6
7   3       b    4   3
8   3       b    5   4
9   4       c    2   6
10  4       c    3   5
11  4       c    4   7
12  4       c    5   3