R

时间:2018-11-23 08:36:23

标签: r dplyr

我有变数。 wer 它具有格式

chr [1:630] "11202 398 2018" "11353 216 2017" "11511 14 2017" "11511 36 2017" "11511 37 2017" "11511 121 2018" ...

所以我不能使用这种格式

          sample_df           first last
1    11202 398 2018  11202 398 2018 <NA>

我想离开sample_df,并用列名隔开空格。 如此预期的输出

x1      x2   x3
11202   398 2018

如何做到?

3 个答案:

答案 0 :(得分:3)

您可以将向量转换为数据帧,然后function parse(path) { return new Promise(function (resolve, reject) { const serv = new Service(); dir.readFiles(path, function (err, content, filename, next) { if (err) return reject(err); //getting files content & preparing/updating service object next(); resolve(serv); }); }); } 将其转换为不同的列

separate

基本R选项为

library(tidyverse)

data_frame(wer) %>%
    separate(wer, into = paste0("x", 1:3), sep = " ") %>%
    mutate_all(as.numeric)


#    x1    x2    x3   
#  <dbl> <dbl> <dbl>
#1 11202 398   2018 
#2 11353 216   2017 
#3 11511 14    2017 
#4 11511 36    2017 

数据

data.frame(do.call("rbind", strsplit(wer, " ")))


#     X1  X2   X3
#1 11202 398 2018
#2 11353 216 2017
#3 11511  14 2017
#4 11511  36 2017

答案 1 :(得分:2)

您可以执行以下操作:

spec:                        
  containers:                
  - name: cas-server-pod     
    image: shiny-cas         
    imagePullPolicy: Never
    command: ["puma -C /app/config/puma.rb"]
    ports:
      - containerPort: 100
    volumeMounts:
      - mountPath: /app/logs
        name: cas-server-logs
      - mountPath: /app/config
        name: cas-server-config
      - mountPath: /app/public
        name: cas-server-public
  volumes:
    - name: cas-server-logs
      hostPath:
        path: /cas-server/logs
    - name: cas-server-config
      hostPath:
        path: /cas-server/config
    - name: cas-server-public
      hostPath:
        path: /cas-server/public

输出:

sample_df <- data.frame(wer = c("11202 398 2018", "11353 216 2017"))

library(tidyverse)

sample_df %>% separate(wer, c("X1", "X2", "X3"))

答案 2 :(得分:1)

我们可以通过base Rread.table中轻松实现此目的

read.table(text = paste(wer, collapse="\n"))
#    V1  V2   V3
#1 11202 398 2018
#2 11353 216 2017
#3 11511  14 2017
#4 11511  36 2017

data.table类似的选项为fread

fread(paste(wer, collapse='\n'))

数据

wer <- c("11202 398 2018", "11353 216 2017", "11511 14 2017", "11511 36 2017")