또한 열 수를 주소 지정합니다. (귀하의 의견에)
여기에 tidyr
과 stringr
을 사용하는 해결책이 있습니다. 그러나 이것은 문자열의 길이가 column2와 같다고 간주합니다. 이 솔루션을 사용하면 행 방향 및 열 방향 계산이 가능합니다. 이것은 매우 기본적인 단계적 방식으로 이루어지며, 몇 줄의 코드로도 동일하게 달성 될 수 있습니다.
library(stringr)
library(tidyr)
data<-data.frame(Column.1 = c("01205", "20705", "27057"),
stringsAsFactors = FALSE)
count<-str_count(data$Column.1) # Get the length of the string in column 2
index<-1:count[1] # Generate an index based on the length
# Count the number of 5 and 7 in each string by row and add it as new column
data$Row.count_5 <- str_count(data$Column.1, "5")
data$Row.count_7 <- str_count(data$Column.1, "7")
new.data <- separate(data, Column.1, into = paste("V", 1:count[1], sep = ""), sep = index)
new.data$'NA' <- NULL
new.data
Column_count_5 <- apply(new.data[1:5],2,FUN=function(x) sum(x == 5))
Column_count_7 <- apply(new.data[1:5],2,FUN=function(x) sum(x == 7))
column_count <- as.data.frame(t(data.frame(Column_count_5,Column_count_7)))
library(plyr)
Final.df<- rbind.fill(new.data,column_count)
rownames(Final.df)<-c("Row1","Row2","Row3", "Column.count_5","Column.count_7")
Final.df
출력
V1 V2 V3 V4 V5 Row.count_5 Row.count_7
Row1 0 1 2 0 5 1 0
Row2 2 0 7 0 5 1 1
Row3 2 7 0 5 7 1 2
Column.count_5 0 0 0 1 2 NA NA
Column.count_7 0 1 1 0 1 NA NA
샘플 데이터
data<-data.frame(Column.1 = c("01205", "20705", "27057"),
stringsAsFactors = FALSE)
공간을 추가하는 것은 관련하지 않는 것을 "나는 그 파일에 대한 몇 가지 통계를 수행 할 삭제 될 수 있습니다 일부 열 또는 행을. " [XY 문제] (http://meta.stackexchange.com/a/66378/203914)가 있습니까? – Roland
각 열에 하나의 문자가 들어 있다는 것을 알고 있다면 요청에 따라 그 문자를 구분할 필요가 없습니다. 각 문자열을 서서 색인 할 수 있습니다. –
그 방법을 보여 주실 수 있습니까? 내 목표는 각 행/열의 5s 및/또는 7s를 확인한 다음 각 열 또는 행을 제거하거나 유지하기로 결정하는 것입니다. –