小编goc*_*lem的帖子

使用R创建原始目标矩阵

我的数据框由个人和他们居住在某个时间点的城市组成.我想为每年生成一个起始 - 目的地矩阵,记录从一个城市到另一个城市的移动数量.我想知道:

  1. 如何自动生成数据集中每年的原始目标表?
  2. 如何以相同的5x5格式生成所有表格,5是我示例中的城市数量?
  3. 是否有比我下面提出的更有效的代码?我打算在一个非常大的数据集上运行它.

请考虑以下示例:

#An example dataframe
id=sample(1:5,50,T)
year=sample(2005:2010,50,T)
city=sample(paste(rep("City",5),1:5,sep=""),50,T)
df=as.data.frame(cbind(id,year,city),stringsAsFactors=F)
df$year=as.numeric(df$year)
df=df[order(df$id,df$year),]
rm(id,year,city)
Run Code Online (Sandbox Code Playgroud)

我最好的尝试

#Creating variables
for(i in 1:length(df$id)){
  df$origin[i]=df$city[i]
  df$destination[i]=df$city[i+1]
  df$move[i]=ifelse(df$orig[i]!=df$dest[i] & df$id[i]==df$id[i+1],1,0) #Checking whether a move has taken place and whether its the same person
  df$year_move[i]=ceiling((df$year[i]+df$year[i+1])/2) #I consider that the person has moved exactly between the two dates at which its location was recorded
}
df=df[df$move!=0,c("origin","destination","year_move")]    
Run Code Online (Sandbox Code Playgroud)

为2007创建原始目标表

yr07=df[df$year_move==2007,]
table(yr07$origin,yr07$destination)
Run Code Online (Sandbox Code Playgroud)

结果

        City1 City2 City3 City5
  City1     0     0     1     2
  City2     2     0     0     0 …
Run Code Online (Sandbox Code Playgroud)

r matrix

7
推荐指数
1
解决办法
1534
查看次数

将函数应用于R中列表元素的所有成对组合

我想将一个函数应用于列表元素的所有成对组合.每个元素都是具有相同长度的向量.我希望输出采用n x n矩阵格式,n即列表中的元素数量.

请考虑以下示例:

# Generating data
l <- list()
for(i in 1:5) l[[i]] <- sample(0:9, 5, T)

# Function to apply
foo <- function(x, y) 1 - sum(x * y) / sqrt(sum(x ^ 2) * sum(y ^ 2))

# Generating combinations
comb <- expand.grid(x = 1:5, y = 1:5)
Run Code Online (Sandbox Code Playgroud)

此循环有效,但速度很慢,输出未格式化为矩阵

# Applying function
out <- list()
for(i in 1:nrow(comb)) {
  out[[i]] <- foo(l[[comb[i, 'x']]], l[[comb[i, 'y']]])
}
Run Code Online (Sandbox Code Playgroud)

任何的想法?

combinations r function list

7
推荐指数
1
解决办法
4896
查看次数

使用R删除名称以模式开头的工作空间对象

我经常创建临时对象,其名称以'tp_'开头并使用用户定义的函数.为了保持干净的工作空间,我想创建一个在保留用户定义的函数的同时删除临时文件的函数.

到目前为止,我的代码是:

rm(list = setdiff(ls(), lsf.str())) # Removes all objects except functions
rm(list = ls(, pattern = "tp_")) # Removes all objects whose name contain 'tp_'
Run Code Online (Sandbox Code Playgroud)

我想要:

  1. 改进第二个函数,以便删除名称以'tp_' 开头的对象(到目前为止,它删除名称中包含 'tp_'的对象).我已经尝试substr(ls(), 1, 3)但不知何故无法将其集成到我的功能中.
  2. 将这两个功能合二为一.

一些R对象:

tp_A = 1
myfun = function(x){sum(x)}
atp_b = 3
Run Code Online (Sandbox Code Playgroud)

该功能应仅从tp_A工作区中删除.

workspace r object

6
推荐指数
1
解决办法
3835
查看次数

使用 R 将列表元素保存为 data.frames

如何将列表中的每个元素保存在单独的 .RData 文件中?

考虑以下示例:

# Generating a list containing 3 matrices

set.seed(1)
mylist=list(M1=matrix(LETTERS[sample(1:26,9)],3),M2=matrix(LETTERS[sample(1:26,9)],3),M3=matrix(LETTERS[sample(1:26,9)],3))
mylist[1:2]

# $M1
# [,1] [,2] [,3]
# [1,] "G"  "U"  "W" 
# [2,] "J"  "E"  "M" 
# [3,] "N"  "S"  "L" 
# 
# $M2
# [,1] [,2] [,3]
# [1,] "B"  "P"  "J" 
# [2,] "F"  "I"  "N" 
# [3,] "E"  "Q"  "R" 

# Transforming the list of matrices into a list of data frames

mylistdf=lapply(mylist,function(x)as.data.frame(x))
Run Code Online (Sandbox Code Playgroud)

我的最佳尝试(不起作用)

lapply(mylistdf,function(x)save(mylistdf[x],file=paste0(getwd(),names(mylistdf)[x],'.RData')))
Run Code Online (Sandbox Code Playgroud)

r list save lapply

4
推荐指数
1
解决办法
3932
查看次数

rbind data.frames 列表,同时在 R 中保留 NULL 元素

我想使用 R将元素转换为rbindNA 。考虑以下示例,listdata.frameNULL

l <- list(data.frame(C1 = 1, C2 = 2),
          NULL,
          data.frame(C1 = 3))

# bind_rows results
dplyr::bind_rows(l)

#   C1 C2
# 1  1  2
# 2  3 NA

# Desired output
data.frame(C1 = c(1, NA, 3), C2 = c(2, NA, NA))

#   C1 C2
# 1  1  2
# 2 NA NA
# 3  3 NA
Run Code Online (Sandbox Code Playgroud)

r list dataframe

2
推荐指数
1
解决办法
2654
查看次数

标签 统计

r ×5

list ×3

combinations ×1

dataframe ×1

function ×1

lapply ×1

matrix ×1

object ×1

save ×1

workspace ×1