我的数据框由个人和他们居住在某个时间点的城市组成.我想为每年生成一个起始 - 目的地矩阵,记录从一个城市到另一个城市的移动数量.我想知道:
请考虑以下示例:
#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) 我想将一个函数应用于列表元素的所有成对组合.每个元素都是具有相同长度的向量.我希望输出采用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)
任何的想法?
我经常创建临时对象,其名称以'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)
我想要:
substr(ls(), 1, 3)但不知何故无法将其集成到我的功能中.一些R对象:
tp_A = 1
myfun = function(x){sum(x)}
atp_b = 3
Run Code Online (Sandbox Code Playgroud)
该功能应仅从tp_A工作区中删除.
如何将列表中的每个元素保存在单独的 .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将元素转换为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)