String 带字符串列的数据帧-每列需要在单词“and”-R处拆分为多个
我有一个具有字符串列的数据帧-每一列的格式都是xyz:x-dffh、dddd和stgL fhgdf 我需要分道扬镳,而休息应该保持原样 输入是一个包含两列的数据帧-输出将针对输入多个输出列中的每列String 带字符串列的数据帧-每列需要在单词“and”-R处拆分为多个,string,r,split,String,R,Split,我有一个具有字符串列的数据帧-每一列的格式都是xyz:x-dffh、dddd和stgL fhgdf 我需要分道扬镳,而休息应该保持原样 输入是一个包含两列的数据帧-输出将针对输入多个输出列中的每列 这在R中可行吗?在excel中,我将使用文本到列-如果“df”是您的数据框,您可以尝试从要拆分的原始列创建两个新列,并根据您的数据调整以下代码: df$newColumn1 <- lapply(strsplit(as.character(df$originalColumn), "and"), "
这在R中可行吗?在excel中,我将使用文本到列-如果“df”是您的数据框,您可以尝试从要拆分的原始列创建两个新列,并根据您的数据调整以下代码:
df$newColumn1 <- lapply(strsplit(as.character(df$originalColumn), "and"), "[", 1)
df$newColumn2 <- lapply(strsplit(as.character(df$originalColumn), "and"), "[", 2)
您可以在基本R中尝试以下类似于bmartinez'z答案的操作,而无需将列表分配给数据帧:
df <- data.frame(originalColumn = c("dog and cat", "robots and raptors"))
do.call(rbind.data.frame, strsplit(as.character(df$originalColumn), "and"))
## > do.call(rbind.data.frame, strsplit(as.character(df$originalColumn), "and"))
## c..dog.....robots... c...cat.....raptors..
## 1 dog cat
## 2 robots raptors
下面是对我有用的东西——使用上面的输入和其他各种线程。我是R的新手,我的目标是将工作从excel迁移到R
# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
#--------------------------------------------------------------------------------
# OBJECTIVE - migrate this activity from excel + VBA to R
#
# split and find out max cols required - each element in dataframe is a list of
#variable length - objective is to convert it into individual columns with number of
#columns = maximum size of list - for the rows with less number of entries the
#additional columns will contain "NA"
---------------------------------------------------------------------------------
temp_split<-strsplit(src.df$PREV,"and")
max_col=max(unlist(lapply(temp_split,length),recursive=TRUE))
# add to dataframe with fixed row and max_col
# keep columns empty - if no data
add_list <- function (x,max_col){
u_l <- unlist(x)
l<-length(unlist(x))
pad_col = max_col - l
r_l <- c(u_l, rep("NA",pad_col))
return(r_l)
}
test<-lapply(temp_split,add_list,max_col)
test_matrix<-data.frame(matrix(unlist(test,recursive=TRUE),nrow=NROW(src.df),byrow=T))
t.df<-test_matrix
c.df<-cbind(src.df,t.df)
这是对Tyler Rinker提供的优秀答案的轻微修改,以解决一个几乎相同的问题。如果希望基于类似于excel中“文本到列”的空间将df分隔为列,该怎么办 试试这个:
df欢迎来到SO。请提供答案,因为它使回答您的问题更容易。您想使用strsplit。更详细的回答将要求您提供dputheadinput,其中输入是您的数据帧。我认为为data.frame列指定列表不是一个好主意。@Roland,只是好奇为什么不这样做?我同意这不是最方便使用的数据格式,但base R的一些函数有时会在聚合等常见操作中使用。主要原因是它会导致不常见的数据结构,这会使代码混乱。这确实几乎是相同的。。。而不是问题的答案。。。无效,因为您已将dfSpace随机放置在第2行末尾。。。而且格式不好。。。
# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
#--------------------------------------------------------------------------------
# OBJECTIVE - migrate this activity from excel + VBA to R
#
# split and find out max cols required - each element in dataframe is a list of
#variable length - objective is to convert it into individual columns with number of
#columns = maximum size of list - for the rows with less number of entries the
#additional columns will contain "NA"
---------------------------------------------------------------------------------
temp_split<-strsplit(src.df$PREV,"and")
max_col=max(unlist(lapply(temp_split,length),recursive=TRUE))
# add to dataframe with fixed row and max_col
# keep columns empty - if no data
add_list <- function (x,max_col){
u_l <- unlist(x)
l<-length(unlist(x))
pad_col = max_col - l
r_l <- c(u_l, rep("NA",pad_col))
return(r_l)
}
test<-lapply(temp_split,add_list,max_col)
test_matrix<-data.frame(matrix(unlist(test,recursive=TRUE),nrow=NROW(src.df),byrow=T))
t.df<-test_matrix
c.df<-cbind(src.df,t.df)