R函数中的局部变量不起作用
Local variables inside R function doesn't work
我需要你的帮助。我的数据框如下所示
id home_1 home_2 home_3
1 1 -0.07288651 -1.0946734 0.06310788
2 2 0.27480575 -0.5939264 -0.10267407
3 3 -1.29267610 -1.0765848 -0.96190129
4 4 -0.53468273 0.5315489 -1.36055340
...
我想创建3个数据框; df1、df2 和 df3
- df1 会有一个 table 和 sorted 列 'home_1'
- df2 会有一个 table 和 sorted 列 'home_2'
- df3 会有一个 table 和 sorted 列 'home_3'
请找到下面的代码
dummy <- data.frame(id = 1:10,home_1 = rnorm(10),home_2 = rnorm(10),home_3 = rnorm(10))
f <- function(df,param1, param2) {
c <- paste0(param1, "_", param2);
print(paste0("Let's sort column ", c))
df %>% arrange(c) %>% print() #sort dataframe by column 'home_1/2/3'
}
for (i in 1:3) {
print(paste0("Index : ",i))
table <- paste0("df",i)
table <- f(dummy,"home",i) # create dataframe with name df1/2/3
}
问题 1
然后我 运行 我的代码,该函数无法检测到相应的列。我函数中的错误
Error in grouped_df_impl(data, unname(vars), drop) :
Column `c` is unknown
局部变量c确实存在,但是group_by函数检测不到c。
Does anybody know how to make Column 'c' to be detected by group_by function?
问题 2
我的 for 循环也有同样的问题。我想创建一个动态的数据框名称。
However, this following function table <- f(dummy,"home",i), created a data frame with name 'table' instead of 'df1'.
任何人都可以提示我如何解决这些问题吗?
提前谢谢你。
您可以遍历列列表,然后按每一列排序
cols <- structure(setdiff(names(dat), "id"), names=setdiff(names(dat), "id"))
lapply(cols, function(x) dat[order(dat[,x]),])
数据:
dat <- read.table(text="id home_1 home_2 home_3
1 -0.07288651 -1.0946734 0.06310788
2 0.27480575 -0.5939264 -0.10267407
3 -1.29267610 -1.0765848 -0.96190129
4 -0.53468273 0.5315489 -1.36055340", header=TRUE)
我会为此使用 tidyverse 的 arrange 函数,这非常简单。
我还会使用 base-r 中的 "assign" 为名字存储在字符串中的向量赋值。
library(tidyverse)
for(i in 1:(ncol(dummy)-1)){
#define the name for the new data
new = paste0("df",i)
#define the same of the column to sort on
col = paste("home",i,sep="_")
# based on the data dummy, arrange the rows according to "col"
# we need to use "get" because arrange expects bare (unquoted) column names
tmp = dummy %>% arrange(get(col))
assign(new, tmp)
}
我需要你的帮助。我的数据框如下所示
id home_1 home_2 home_3
1 1 -0.07288651 -1.0946734 0.06310788
2 2 0.27480575 -0.5939264 -0.10267407
3 3 -1.29267610 -1.0765848 -0.96190129
4 4 -0.53468273 0.5315489 -1.36055340
...
我想创建3个数据框; df1、df2 和 df3
- df1 会有一个 table 和 sorted 列 'home_1'
- df2 会有一个 table 和 sorted 列 'home_2'
- df3 会有一个 table 和 sorted 列 'home_3'
请找到下面的代码
dummy <- data.frame(id = 1:10,home_1 = rnorm(10),home_2 = rnorm(10),home_3 = rnorm(10))
f <- function(df,param1, param2) {
c <- paste0(param1, "_", param2);
print(paste0("Let's sort column ", c))
df %>% arrange(c) %>% print() #sort dataframe by column 'home_1/2/3'
}
for (i in 1:3) {
print(paste0("Index : ",i))
table <- paste0("df",i)
table <- f(dummy,"home",i) # create dataframe with name df1/2/3
}
问题 1 然后我 运行 我的代码,该函数无法检测到相应的列。我函数中的错误
Error in grouped_df_impl(data, unname(vars), drop) :
Column `c` is unknown
局部变量c确实存在,但是group_by函数检测不到c。
Does anybody know how to make Column 'c' to be detected by group_by function?
问题 2 我的 for 循环也有同样的问题。我想创建一个动态的数据框名称。
However, this following function table <- f(dummy,"home",i), created a data frame with name 'table' instead of 'df1'.
任何人都可以提示我如何解决这些问题吗? 提前谢谢你。
您可以遍历列列表,然后按每一列排序
cols <- structure(setdiff(names(dat), "id"), names=setdiff(names(dat), "id"))
lapply(cols, function(x) dat[order(dat[,x]),])
数据:
dat <- read.table(text="id home_1 home_2 home_3
1 -0.07288651 -1.0946734 0.06310788
2 0.27480575 -0.5939264 -0.10267407
3 -1.29267610 -1.0765848 -0.96190129
4 -0.53468273 0.5315489 -1.36055340", header=TRUE)
我会为此使用 tidyverse 的 arrange 函数,这非常简单。
我还会使用 base-r 中的 "assign" 为名字存储在字符串中的向量赋值。
library(tidyverse)
for(i in 1:(ncol(dummy)-1)){
#define the name for the new data
new = paste0("df",i)
#define the same of the column to sort on
col = paste("home",i,sep="_")
# based on the data dummy, arrange the rows according to "col"
# we need to use "get" because arrange expects bare (unquoted) column names
tmp = dummy %>% arrange(get(col))
assign(new, tmp)
}