简体   繁体   English

如何修剪数据帧列表中的所有空间R

[英]How to Trim all what space from a list of data frames R

the data that I am ingesting in my R script are full of white spaces (the bane of my existance). 我在R脚本中提取的数据充满了空白(这是我生存的祸根)。 Thus far I have been using trimws within my functions so that my joins return true results. 到目前为止,我一直在函数中使用修剪,以便我的联接返回真实结果。 I am wondering if it is possible trim the white space in all columns and all data frames that I have stored in a list. 我想知道是否有可能修剪存储在列表中的所有列和所有数据帧中的空白区域。

ParsedFile <- grep("ItemDetail", names(.GlobalEnv), value = TRUE)

this creates a list of the data frames that I want to remove the white space of in all the fields.I thought this would work but lapply does not seem to want to write the information back to the data frame. 这会创建一个数据帧列表,我想删除所有字段中的空白。我认为这会起作用,但是lapply似乎不想将信息写回到数据帧。

as.data.frame(lapply(get(ParsedFile), trimws))

Moreover, I see it only print 1 result to the console where I expect a result for each data frame. 而且,我看到它仅将1个结果打印到控制台,在控制台中我希望每个数据帧都有结果。

Can someone please help me out? 有人可以帮我吗?

Thanks 谢谢

Use purrr and its map function to iterate over the list of data frames, then map_df to iterate over the columns in each data frame, which will return the results as data_frames . 使用purrr及其map函数迭代数据帧列表,然后使用map_df迭代每个数据帧中的列,这将返回结果为data_frames

library(purrr)
ParsedFile %>% map(~map_df(., ~trimws(.)))

IIUC, here's a way to do it. IIUC,这是一种实现方法。 I don't know what your desired output is. 我不知道您想要的输出是什么。 But, this method returns a list of dataframe. 但是,此方法返回一个数据帧列表。 This might give you some thoughts: 这可能会给您一些想法:

# here df1, df2 are data frames
df1 = data.frame(name = c(' mani ','san ',' fdfg '))
df2 = data.frame(name = c(' mani ','gh ',' fdfg '))

# do this
lapply(c(df1, df2), function(x) do.call(rbind, lapply(x, trimws)))

$name
     [,1]  
[1,] "mani"
[2,] "san" 
[3,] "fdfg"

$name
     [,1]  
[1,] "mani"
[2,] "gh"  
[3,] "fdfg"

Second Method: For your case :- 第二种方法:对于您的情况:-

# save intermediate list
temp <- lapply(get(ParsedFile), function(x) do.call(rbind, lapply(x, trimws)))

# convert list to df
new_df <- data.frame(new_col = do.call(rbind, temp))

print(new_df)

    new_col
1    mani
2     san
3    fdfg
4    mani
5      gh
6    fdfg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM