如何从R中的两个不同列提取和组合字符串

时间:2017-04-14 04:23:50

标签: r

我想知道如果有多个无序变量,我如何组合R中两个不同列的字符串? 具体来说,如果我有一组这样的数据:

vec=haversine(line_station.D_Lng, line_station.D_Lat, row2.O_Lng, row2.O_Lat)
idx = np.argmin(vec)
minimum=vec[idx]

如何对它们进行分组并获得一个新的表格,如:

1 | R~^~C                  4~^~5

2 | L~^~C~^~S            5~^~5~^~5

3 | S~^~R                    5~^~4

4 | V~^~L~^~S~^~R~^~C        5~^~4~^~5~^~3~^~5

...

提前谢谢!

2 个答案:

答案 0 :(得分:0)

需要一个有效的例子:

txt <- "1 | R~^~C                  4~^~5
2 | L~^~C~^~S            5~^~5~^~5
3 | S~^~R                    5~^~4
4 | V~^~L~^~S~^~R~^~C        5~^~4~^~5~^~3~^~5"
 d <- read.table(text=txt)

他们构建一个数据框来保存将由列名称确定的值(在第4列中)(在第3列中):

colnames <- sapply( gsub("~^~", "," , as.character(d$V3), fixed=TRUE), 
                     function(x)scan(text=x, what="", sep=",") )
values <- sapply( gsub("~^~", "," , as.character(d$V4), fixed=TRUE), 
                   function(x)scan(text=x, what=numeric(), sep=",") )

target <- data.frame(NA,NA,NA,NA,NA) # Could vary the order without loss of generality
colnames(target) <- unique(unlist(colnames))
for ( i in seq_along(colnames) ){ 
                         target[i, colnames[[i]] ] <- values[[i]]}
> target
   R  C  L  S  V
1  4  5 NA NA NA
2 NA  5  5  5 NA
3  4 NA NA  5 NA
4  3  5  4  5  5

答案 1 :(得分:0)

d <- read.table(text = "1 | R~^~C                  4~^~5
2 | L~^~C~^~S            5~^~5~^~5
3 | S~^~R                    5~^~4
4 | V~^~L~^~S~^~R~^~C        5~^~4~^~5~^~3~^~5", as.is = TRUE)

colNames <- unique(unlist(strsplit(d$V3, '\\~\\^\\~')))

paired <- t(apply(d[, 3:4], 1, function(x){
  spli <- strsplit(x, '\\~\\^\\~')
  tab <- cbind(spli[[1]], spli[[2]])
  out <- rep(NA, length(colNames))
  out[match(spli[[1]], colNames)] <- spli[[2]]
  names(out) <- colNames
  return(out)
}))