将字符串拆分为 r 中的六个新变量
Splitting a string into six new variables in r
我正在尝试将一串数字拆分为六个不同的变量。我尝试在原来的 df 中这样做,但它给我带来了很多问题,所以我决定提取我需要拆分成一个临时数据框的列(目的是一旦变量正确的地方):
statusTemp <- select(recruitDF, Status)
tail(statusTemp, n = 15)
Status
486 109 ; 0 ; 0 ; 22 ; 0 ; 7
487 63 ; 0 ; 0 ; 2 ; 0 ; 3
488 93 ; 0 ; 0 ; 4 ; 0 ; 2
489 42 ; 0 ; 0 ; 3 ; 0 ; 2
490 13 ; 0 ; 0 ; 5 ; 0 ; 1
491 50 ; 0 ; 0 ; 1 ; 0 ; 3
492 10 ; 0 ; 0 ; 2 ; 0 ; 1
493 56 ; 0 ; 0 ; 3 ; 0 ; 2
494 40 ; 0 ; 0 ; 3 ; 0 ; 0
495 35 ; 0 ; 0 ; 10 ; 0 ; 0
496 134 ; 0 ; 0 ; 5 ; 0 ; 1
497 12 ; 0 ; 0 ; 2 ; 0 ; 1
498 30 ; 0 ; 0 ; 0 ; 0 ; 2
499 49 ; 0 ; 0 ; 6 ; 0 ; 4
500 11 ; 0 ; 0 ; 0 ; 0 ; 0
然后我尝试使用 reshape2
中的 colsplit
将温度拆分为六个新的和适当的变量,但我在某个地方搞砸了,我不明白为什么。
library(reshape2)
library(stringr)
statusTemp <- colsplit(statusTemp, ";", names = c("Application",
"Screening",
"Test",
"Interview",
"References",
"Hired"))
str(statusTemp)
'data.frame': 1 obs. of 6 variables:
$ Application: chr "c(1, 1, 1, 1, 1, 1, 1, 13, 1, 13, 188, 13, 1, 13, 188, 118, 118, 275, 188, 1, 13, 188, 337, 13, 355, 188, 118, 246, 64, 246, 64"| __truncated__
$ Screening : logi NA
$ Test : logi NA
$ Interview : logi NA
$ References : logi NA
$ Hired : logi NA
谁能帮我弄清楚我忽略了什么或做错了什么?
library(dplyr)
library(stringr)
statusTemp %>% separate(Status, c("1", "2", "3", "4", "5", "6"), ";") %>%
mutate_all(funs(str_trim)) # to remove both leading and trailing whitespace
您的代码运行良好。正如@Cath 所述,您需要使用 statusTemp$Status
而不是 statusTemp
。这是一个例子。
library(reshape2)
colsplit(df$Status, ";", names = c("Application",
"Screening",
"Test",
"Interview",
"References",
"Hired"))
# output
# Application Screening Test Interview References Hired
#1 109 0 0 22 0 7
#2 63 0 0 2 0 3
#3 93 0 0 4 0 2
#4 42 0 0 3 0 2
#...
# data
structure(list(Status = structure(c(2L, 14L, 15L, 10L, 5L, 12L,
1L, 13L, 9L, 8L, 6L, 4L, 7L, 11L, 3L), .Label = c(" 10 ; 0 ; 0 ; 2 ; 0 ; 1 ",
" 109 ; 0 ; 0 ; 22 ; 0 ; 7 ", " 11 ; 0 ; 0 ; 0 ; 0 ; 0",
" 12 ; 0 ; 0 ; 2 ; 0 ; 1 ", " 13 ; 0 ; 0 ; 5 ; 0 ; 1 ",
" 134 ; 0 ; 0 ; 5 ; 0 ; 1 ", " 30 ; 0 ; 0 ; 0 ; 0 ; 2 ",
" 35 ; 0 ; 0 ; 10 ; 0 ; 0 ", " 40 ; 0 ; 0 ; 3 ; 0 ; 0 ",
" 42 ; 0 ; 0 ; 3 ; 0 ; 2 ", " 49 ; 0 ; 0 ; 6 ; 0 ; 4 ",
" 50 ; 0 ; 0 ; 1 ; 0 ; 3 ", " 56 ; 0 ; 0 ; 3 ; 0 ; 2 ",
" 63 ; 0 ; 0 ; 2 ; 0 ; 3 ", " 93 ; 0 ; 0 ; 4 ; 0 ; 2 "
), class = "factor")), .Names = "Status", class = "data.frame", row.names = c(NA,
-15L))
我正在尝试将一串数字拆分为六个不同的变量。我尝试在原来的 df 中这样做,但它给我带来了很多问题,所以我决定提取我需要拆分成一个临时数据框的列(目的是一旦变量正确的地方):
statusTemp <- select(recruitDF, Status)
tail(statusTemp, n = 15)
Status
486 109 ; 0 ; 0 ; 22 ; 0 ; 7
487 63 ; 0 ; 0 ; 2 ; 0 ; 3
488 93 ; 0 ; 0 ; 4 ; 0 ; 2
489 42 ; 0 ; 0 ; 3 ; 0 ; 2
490 13 ; 0 ; 0 ; 5 ; 0 ; 1
491 50 ; 0 ; 0 ; 1 ; 0 ; 3
492 10 ; 0 ; 0 ; 2 ; 0 ; 1
493 56 ; 0 ; 0 ; 3 ; 0 ; 2
494 40 ; 0 ; 0 ; 3 ; 0 ; 0
495 35 ; 0 ; 0 ; 10 ; 0 ; 0
496 134 ; 0 ; 0 ; 5 ; 0 ; 1
497 12 ; 0 ; 0 ; 2 ; 0 ; 1
498 30 ; 0 ; 0 ; 0 ; 0 ; 2
499 49 ; 0 ; 0 ; 6 ; 0 ; 4
500 11 ; 0 ; 0 ; 0 ; 0 ; 0
然后我尝试使用 reshape2
中的 colsplit
将温度拆分为六个新的和适当的变量,但我在某个地方搞砸了,我不明白为什么。
library(reshape2)
library(stringr)
statusTemp <- colsplit(statusTemp, ";", names = c("Application",
"Screening",
"Test",
"Interview",
"References",
"Hired"))
str(statusTemp)
'data.frame': 1 obs. of 6 variables:
$ Application: chr "c(1, 1, 1, 1, 1, 1, 1, 13, 1, 13, 188, 13, 1, 13, 188, 118, 118, 275, 188, 1, 13, 188, 337, 13, 355, 188, 118, 246, 64, 246, 64"| __truncated__
$ Screening : logi NA
$ Test : logi NA
$ Interview : logi NA
$ References : logi NA
$ Hired : logi NA
谁能帮我弄清楚我忽略了什么或做错了什么?
library(dplyr)
library(stringr)
statusTemp %>% separate(Status, c("1", "2", "3", "4", "5", "6"), ";") %>%
mutate_all(funs(str_trim)) # to remove both leading and trailing whitespace
您的代码运行良好。正如@Cath 所述,您需要使用 statusTemp$Status
而不是 statusTemp
。这是一个例子。
library(reshape2)
colsplit(df$Status, ";", names = c("Application",
"Screening",
"Test",
"Interview",
"References",
"Hired"))
# output
# Application Screening Test Interview References Hired
#1 109 0 0 22 0 7
#2 63 0 0 2 0 3
#3 93 0 0 4 0 2
#4 42 0 0 3 0 2
#...
# data
structure(list(Status = structure(c(2L, 14L, 15L, 10L, 5L, 12L,
1L, 13L, 9L, 8L, 6L, 4L, 7L, 11L, 3L), .Label = c(" 10 ; 0 ; 0 ; 2 ; 0 ; 1 ",
" 109 ; 0 ; 0 ; 22 ; 0 ; 7 ", " 11 ; 0 ; 0 ; 0 ; 0 ; 0",
" 12 ; 0 ; 0 ; 2 ; 0 ; 1 ", " 13 ; 0 ; 0 ; 5 ; 0 ; 1 ",
" 134 ; 0 ; 0 ; 5 ; 0 ; 1 ", " 30 ; 0 ; 0 ; 0 ; 0 ; 2 ",
" 35 ; 0 ; 0 ; 10 ; 0 ; 0 ", " 40 ; 0 ; 0 ; 3 ; 0 ; 0 ",
" 42 ; 0 ; 0 ; 3 ; 0 ; 2 ", " 49 ; 0 ; 0 ; 6 ; 0 ; 4 ",
" 50 ; 0 ; 0 ; 1 ; 0 ; 3 ", " 56 ; 0 ; 0 ; 3 ; 0 ; 2 ",
" 63 ; 0 ; 0 ; 2 ; 0 ; 3 ", " 93 ; 0 ; 0 ; 4 ; 0 ; 2 "
), class = "factor")), .Names = "Status", class = "data.frame", row.names = c(NA,
-15L))