• Register
1 vote
376 views

Problem :

I am very new to R. Currently I want to split the column of my data frame (menuA) is as shown below

Column1
1|3|4|5
4|5|6|7

I was trying to split my numbers in above columnA so I coded it as shown below.

menuA <- data.frame (do.call('rbind', strsplit(as.character(myCmenu$myFile.menudata), '|', fixed = TRUE)))

And I got the as expected result which is as shown below:

col1| col2 | col3 | col4
  1  |  3   |  4   | 5
  4  |  5   |  6   | 7

But I am facing below warning message from the R

> Warning message:
In rbind(c("", "164200", "", "167", "108", "112", "116", "120"),  :
  number of columns of result is not a multiple of vector length (arg 1)

Please let me know if all my data is separated properly?

9 6 2
3,980 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution :

I have experience in R and I found your problem interesting so I am trying to help you.

I saw your example so based on it you can use cSplit as shown below for your reference.

library(splitstackshape)
cSplit(menuA, "Column1", "|")
#   Column1_1 Column1_2 Column1_3 Column1_4
#1:         1         3         4         5
#2:         4         5         6         7
Or you can use below method
read.table/read.csv can be used.

read.table(text=menuA$Column1, sep="|", fill=TRUE, header=FALSE)
If you use any of the above methods your problem will be resolved.
5 4 2
17,150 points
0 votes

Solution:

Differents lengths in original x, x1 and z lists is that the initial drawback, however are you able to say that z values is said to every x and x1 values?

x <- c(-10, 1:6, 50)
x1<- c(-20, 1:5, 60)
z<- c(1,2,3,4,5,6,7,8)
length(x)
[1] 8
length(x1)
[1] 7
length(z)
[1] 8

Another problem is here:

x<- x[!(x %in% bx$out)] #remove this
x1 <- x1[!(x1 %in% bx1$out)] #remove this


x_to_remove<-which(x %in% bx$out)
x <- x[!(x %in% bx$out)]

x1_to_remove<-which(x1 %in% bx1$out)
x1 <- x1[!(x1 %in% bx1$out)]

You clean x and x1 before calculating x_to_remove and x1_to_remove

EDIT: To achieve your desired output try this code (/ode lines added signed in comments):

x <- c(-10, 1:6, 50)
x1<- c(-20, 1:5, 60)
z<- c(1,2,3,4,5,6,7,8)

length_max<-min(length(x),length(x1),length(z)) #Added: identify max length before outlier detection

bx <- boxplot(x)
bx1 <- boxplot(x1)

x_to_remove<-which(x %in% bx$out)
x <- x[!(x %in% bx$out)]

x1_to_remove<-which(x1 %in% bx1$out)
x1 <- x1[!(x1 %in% bx1$out)]

z<-z[-unique(c(x_to_remove,x1_to_remove))]

length_min<-min(length(x),length(x1),length(z)) #Minimum length after outlier remove

d=data.frame(cbind(x[1:length_min],x1[1:length_min],z[1:length_min])) #Bind columns
colnames(d)<-c("x","x1","z")

d_NA<-as.data.frame(matrix(rep(NA,(length_max-length_min)*3),nrow=(length_max-length_min))) #Create NA rows
 colnames(d_NA)<-c("x","x1","z")

d<-rbind(d,d_NA) #Your desired output
d
   x x1  z
1  1  1  2
2  2  2  3
3  3  3  4
4  4  4  5
5  5  5  6
6 NA NA NA
7 NA NA NA

Based on the example provided, either cSplit

library(splitstackshape)
cSplit(menuD, "Column1", "|")
#   Column1_1 Column1_2 Column1_3 Column1_4
#1:         1         3         4         5
#2:         4         5         6         7

Or separate from tidyr

library(tidyr)
separate(menuD, Column1, into = paste0("col", 1:4))

Or read.table/read.csv can be used.

read.table(text=menuD$Column1, sep="|", fill=TRUE, header=FALSE)

But, the warning in the OP's post indicates that there might be elements in the "Column1" with less number of |. In that issue, the cSplit or the last option should work.

10 6 4
31,120 points

Related questions

0 votes
1 answer 17 views
17 views
Problem: I have new question related with this my topic deleting outlier in r with account of nominal var. In new case variables x and x1 has different lenght x <- c(-10, 1:6, 50) x1<- c(-20, 1:5, 60) z<- c(1,2,3,4,5,6,7,8) bx <- boxplot(x) bx$out bx1 ... is not help me Rsolnp: In cbind(temp, funv) : number of rows of result is not a multiple of vector length (arg 1) or i just do anything wrong.
asked May 2 padma1 34.8k points
0 votes
1 answer 9 views
9 views
Problem: Support is needed > Number of rows of result is not a multiple of vector length .
asked May 8 Isac Christiaan 9.6k points
1 vote
1 answer 110 views
110 views
Problem : Currently I am in the process of learning the Dataframe and now I have the dataframe combination which includes the two variables DT and OD. I also have very few missing values like NA in both of the DT and OD but not necessary they have the same ... not very sure why two columns in a same dataframe will have the different length. Please guide me in fixing above dataframe related error.
asked May 12, 2020 stewart 4k points
0 votes
1 answer 8 views
8 views
Problem: i don&rsquo;t understand this problem. please help me? names' attribute [2] must be the same length as the vector [1]
asked Apr 10 Rohit kr 17.5k points
1 vote
1 answer 8 views
8 views
Problem: Can somebody help me ? Why am I gettign warning message in these two lines: > memb = dih_y2$MemberID[1:10] > dih_col = which(dih_y2$MemberID == memb) Warning message: In dih_y2$MemberID == memb : longer object length is not a multiple of shorter object length
asked May 3 Raj Tyagi 15.6k points
1 vote
1 answer 11 views
11 views
Problem: I am getting a warning while workig on R: Longer object length is not a multiple of shorter object length.
asked May 1 PkGuy 23.5k points
0 votes
1 answer 8 views
8 views
Question: I don't understand why R gives me a warning about "Longer object length is not a multiple of shorter object length" I have this object which is generated by doing an aggregate over an xts series giving the weekday median: u <- aggregate(d, list(Ukedag = ... in a warning. The intent is to subtract the weekday mean. It appears to work despite the warning, but what should I worry about?
asked Apr 18 munim01 21k points
1 vote
1 answer 224 views
224 views
Problem: I am very new to R programming. I have recently started learning R programming with the help of the online tutorials and I am also referring few books available on R programming. I have my one dataframe named dih_y2. Below mentioned two lines are giving me ... length I am unable to understand above error due to my limited knowledge on R. Kindly guide me in fixing above R related error.
asked May 27, 2020 Martin K 6.6k points
1 vote
1 answer 1.1K views
1.1K views
Problem : Currently I am learning r. I have the data table "normal" which has the numeric as far as I know normalized values. If I try to execute the below command k <- kmeans(normal,center=3) Then I always face the below error: &ldquo;Error in do_one(nmeth) : NA/NaN/ ... call (arg 1)&rdquo; I am trying to fix it by researching on it but still unable to fix it. Can somebody help me in fixing it?
asked Mar 28, 2020 morrisBson 3.2k points
0 votes
1 answer 259 views
259 views
Problem : I have done a lot of research on this extensively without finding any solution on it. I have tried cleaning my data set as follows: library("myraster") impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l) , mean(l, na.rm = TRUE)) losses <- ... TRUE, na.rm=TRUE, nan.rm=TRUE) All my research says it should be NA's or Inf's or NaN's in the data but I don't have any
asked Dec 5, 2019 alecxe 7.5k points