Subscribe to DSC Newsletter

Add column values as new row conditionally

I csv data file in below format:

1:

101

102

2:

103

104

105

3:

106

107

I want result as below in R

Value     id

101         1

102         1

103         2

104         2

105         2

106         3

107         3

I did using for loop but it takes lot of time. Original data 3MB file.

Added extra column "m_id" in test_data dataframe and executed below code.

f <- function(x){
value = 0
if(endsWith(as.character(x) ,":") == TRUE){
value = substr(x,0,nchar(x)-1)
}
return (value)
}

for(row in 1:nrow(test_data)){
id = 0
id = f(as.character(test_data[row,1]))
if(id != 0){
m_id = id
}
test_data[row,2]<-m_id
}

How i can achieve the above requirement without for loop?

Views: 136

Reply to This

Replies to This Discussion

Let me answer myself in case if some one else face the same issue :)

df <- data_frame(text=junk) df %>%   
separate_rows
(text, sep = '\n') %>%
filter
(text != "") %>%
extract
(text, into = c('group'), regex = "^(\\d+):", remove = FALSE) %>%
fill
(group) %>%
filter
(!grepl("^\\d+:", text)) %>%
select
(group, text)

I applied this on my data frame using below code and worked as expected.

test_data is data frame and CustomerId is column having same format data which i posted in my query.

test <- extract(data = test_data,col = CustomerId, into = c('group'), regex = "^(\\d+):", remove = FALSE) %>%
fill(group) %>% filter(!grepl("^\\d+:", CustomerId)) %>% select(group, CustomerId)


Reply to Discussion

RSS

Videos

  • Add Videos
  • View All

Follow Us

© 2018   Data Science Central ®   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service